티스토리 뷰

카테고리 없음

[정렬] Bubble sort

DEV LION 2010. 1. 9. 15:57

#include <stdio.h>
#define ARRYSIZ 200

void bubbleSort(int*,int);                                                                // bubbleSort
void prntData(int*,int);                                                                // 배열의 내용 출력

void main(){

        int siz = 8, dataAry[ARRYSIZ] = {2,0,0,6,5,5,5,4};        // data배열, data의 크기
        printf("\nBefore bubble sort:\n");
        prntData(dataAry,siz);                                                                // 배열의 내용 출력
        printf("\n\n");
        bubbleSort(dataAry,siz);                                                        // bubbleSort
        printf("\nAfter bubble sort:\n");
        prntData(dataAry,siz);                                                                // 배열의 내용 출력
}

void prntData(int* dataAry,int siz){                                        // 배열의 내용 출력

        int i=0;
        while(i<siz){
                printf("[%d] ",dataAry[i++]);
        }
        printf("\n");
}

void bubbleSort(int* dataAry, int siz){                                        // bubble sort

        int i,j,temp;
        for(i=siz-1;i>=0;i--){
                for(j=0;j<i;j++){
                        if(dataAry[j]>dataAry[j+1]){                //뒤에 값과 비교하여 크면
                                temp = dataAry[j+1];                        //값을 교환한다.
                                dataAry[j+1] = dataAry[j];
                                dataAry[j] = temp;
                        }
                }
                        prntData(dataAry,siz);                                                                // 배열의 내용 출력
                        printf("\n");
        }

}

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함