C Bubble Sort로 오름차순 정렬하는 방식입니다.
시간복잡도 : O(n^2)
#include <stdio.h>
void printArray(int value[], int count) {
int i = 0;
for (i = 0; i < count; i++) {
printf("%d ", value[i]);
}
printf("\n");
}
void bubbleSort(int value[], int count) {
int i = 0, j = 0, temp = 0;
for (i = count - 1; i >= 0; i--) {
for (j = 0; j <= i; j++) { // i 뒤쪽은 다 정렬이 된 자료들이 남아있게 되므로, j <= i 가 된다.
if (value[j - 1] > value[j]) {
temp = value[j - 1];
value[j - 1] = value[j];
value[j] = temp;
}
}
printf("Step-%d, ", count - i);
printArray(value, count);
}
}
int main(int argc, char *argv[]) {
int values[] = { 80, 50, 70, 10, 60, 20, 40, 30 };
printf("Before BubbleSort\n");
printArray(values, 8);
bubbleSort(values, 8);
printf("\nAfter Sort\n");
printArray(values, 8);
return 0;
}
'Programming > C' 카테고리의 다른 글
Sorting(3) Selection Sort(선택 정렬) (0) | 2016.11.01 |
---|---|
Sorting(2) Insertion Sort(삽입 정렬) (0) | 2016.11.01 |
(6) CircularList 사용하기 (0) | 2016.10.30 |
(5) DoubleLinkedList 사용하기 (0) | 2016.10.30 |
(4) Linkedlist 를 이용하여 Reverse, Concat 응용 (0) | 2016.10.30 |
WRITTEN BY