본문 바로가기
C Programming

1차원 배열을 활용한 버블 정렬[Bubble Sort]

by TYB 2023. 10. 14.
반응형
#if 1// bubble sort 
#include <stdio.h>
#define BUF_SIZE 5


void swap(int *pa, int *pb);



void bubbleSort(int* arr){
    int i,j;
    for(i=0; i < BUF_SIZE; i++){
        for(j=0; j<BUF_SIZE-i-1; j++){
            if(arr[j] > arr[j+1]){    // swap
                swap(arr+j,arr+j+1);
            }
        }
    }
}
 
int main(){
    int arr[BUF_SIZE] = {7,5,1,4,3};
    int i;
    printf("정렬 전 : ");
    for(i=0; i<5; i++){
        printf("%d ", arr[i]);
    }
    printf("\n");
    
    bubbleSort(arr);
    printf("정렬 후 : ");
    for(i=0; i<5; i++){
        printf("%d ", arr[i]);
    }
    printf("\n");    
    
    return 0; 
}
void swap(int *pa, int *pb)
{
	int temp;
	temp =*pa;
	*pa = *pb;
	*pb = temp;
}
#endif
반응형