알고리즘
[정렬] 퀵 정렬(C언어 정렬 함수)
parkit
2018. 9. 21. 02:11
728x90
반응형
C언어의 <stdlib.h>에서 제공하는 퀵 정렬 함수이다.
최악 : O(N^2)
보통 : O(NlogN)
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 | #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> // 오름차순으로 정렬할 때 사용하는 비교함수 int static compare(const void* first, const void* second) { if (*(int*)first > *(int*)second) return 1; // 내림차순은 -1 else if (*(int*)first < *(int*)second) return -1; // 내림차순은 1 else return 0; } int main() { int arr[] = { 32, 11, 97, 42, 21, 70, 56, 67, 88, 100 }; int array_size = sizeof(arr) / sizeof(int); int i; // 정렬 전 for (i = 0; i < array_size; i++) printf("%d ", arr[i]); printf("\n"); qsort(arr, array_size, sizeof(int), compare); // 정렬 후 for (i = 0; i < array_size; i++) printf("%d ", arr[i]); printf("\n"); return 0; } | cs |
728x90
반응형