버블정렬

언어/c언어2016. 6. 12. 14:20



1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
int main(void) {
    int num = 10;
    int* pnum = &num;
    int** ppnum = &pnum;
 
    printf(" addr(num) -> %p / pnum -> %p \n"&num, pnum);
    printf(" ppnum -> %p / addr(pnum) -> %p\n", ppnum, &pnum);
    printf(" *ppnum -> %p / pnum -> %p\n"*ppnum, pnum);
    printf(" **ppnum -> %d / num -> %d \n", (**ppnum), num);
    return 0;
}
cs












1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct _bubble {
    int _size_;
    int* Array;
}bubble;
// Func prototype
void __init__(bubble** b);
void __dataInput__(bubble** b);
void __before__(bubble** b);
void __bubbleSort__(bubble** b);
// ______________main__________________
int main(void) {
    bubble* stu = NULL;
    __init__(&stu);
    __dataInput__(&stu);
    __before__(&stu);
    __bubbleSort__(&stu);
    return 0;
}
 
void __init__(bubble** b) {
    (*b) = (bubble*)malloc(sizeof(bubble));
    (**b)._size_ = 0;
    (**b).Array = NULL;
// end of __init_i
void __dataInput__(bubble** b) {
    int i; // index
    printf(" 배열의 사이즈 입력 : ");
    scanf("%d"&((**b)._size_));
 
    (**b).Array = (int*)malloc(sizeof(int)* (**b)._size_);
    memset( (**b).Array ,0sizeof(int)* (**b)._size_ );
 
    for (i = ; i < (*b)->_size_ ; i++) {
        printf" Array[%d] : ", i);
        scanf"%d", ((*b)->Array + i));
    }
// end of __dataInput__
void __before__(bubble** b) {
    int i;
    printf("=============== 정렬 전 =============== \n");
    for (i = ; i < (**b)._size_ ; i++) {
        printf("[%d] "*((*b)->Array + i) );
    } printf("\n");
    printf("====================================== \n");
}
void __bubbleSort__(bubble** b) {
    int i, j, k; // index
    int result;
    int temp = 0;
    int count = 0;
    for (i = ; i < (**b)._size_ - 1; i++) {
        result = 0;
        for (j = ; j <(**b)._size_ - 1; j++) {
            if ( *((*b)->Array + j) > *((*b)->Array + (j+1)) ) {
                temp = *((*b)->Array + j);
                *((*b)->Array + j) = *((*b)->Array + (j+1));
                *((*b)->Array + (j+1)) = temp;
                count++;
                result = 1;
            }
        }
        if (result == 1) {
            for (k = ; k < (**b)._size_ ; k++) {
                printf("[%d] "*((*b)->Array + k) );
            } printf("\n");
        } else { // result == 0
            
            break;
        }
    }
    printf("=============== 정렬 후 =============== \n");
    for (k = ; k < (**b)._size_ ; k++) {
        printf("[%d] "*((*b)->Array + k) );
    } printf("\n");
    printf("====================================== \n");
    printf("정렬 횟수 : %d \n", count);
}
cs





'언어 > c언어' 카테고리의 다른 글

앞 문자열 대문자 로 ~~~  (0) 2016.06.17
c언어 조건에 맞는 사람 찾기  (0) 2016.06.12
369 게임 (네이버 답변)  (0) 2016.06.06
별찍기 관련 c언어  (0) 2016.06.06
test  (0) 2016.06.02