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 32 33 34 35
|
void my_qsort(int array[], int size, int(*choose_pivot)(int[], int)){
my_qsort(array, 0, size - 1, choose_pivot);
}
void my_qsort(int array[], int low, int high, int(*choose_pivot)(int[], int)){
int i = low;
int j = high;
int size = high - low;
int tmp;
if (size > 1){
int pivot = array[choose_pivot(array, size)];
//int pivot = array[rand() % size + i];
//int pivot = array[0];
//int pivot = array[size/2];
while (i < j) {
while (array[j] > pivot && j > i) {
j--;
}
while (array[i] < pivot && i <= j) {
i++;
}
if (i < j){
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
}
}
my_qsort(array, low, i, choose_pivot);
my_qsort(array, j, high, choose_pivot);
}
else
return;
}
|