// sort.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LENGTH 10000000
int cmpint (const void *pa, const void *pb) {
int a = *(int*)pa;
int b = *(int*)pb;
return a < b ? -1 : a == b ? 0 : 1;
}
int main (void) {
int *arr = malloc(LENGTH * sizeof(int));
for (int i = 0; i < LENGTH; i++) {
arr[i] = i;
}
clock_t start = clock();
qsort(arr, LENGTH, sizeof(int), cmpint);
double t = (double)(clock() - start) / CLOCKS_PER_SEC;
printf("qsort: %fs\n", t);
return 0;
}
// gcc sort.c -o sort -O3 -march=native && ./sort
// qsort: 0.368455s
알고리즘의 차이가 있다고 하지만 C언어는 이미 정렬되어 있는 배열을 던져줬는데도 그냥 정렬하는 자바가 더 빠름 (자바: 0.013825932s)
퀵정렬 이미 어느정도 정렬되어있으면 최악이잖아