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 36 37 38 39 40
| #ifndef ALGORITHM_QUICK_SORT_H #define ALGORITHM_QUICK_SORT_H
int partition (int array[], int left, int right) { int i, j, pivot; i = left; j = right + 1; pivot = left;
do { do { i++; } while (array[i] < array[pivot]); do { j--; } while (array[pivot] < array[j]); if (i < j) { swap(&array[i], &array[j]); } } while (i < j);
swap(&array[pivot], &array[j]);
return j; }
void quick_sort (int array[], int left, int right) { int pivot;
if (left < right) { pivot = partition(array, left, right); quick_sort(array, left, pivot-1); quick_sort(array, pivot+1, right); } print_array(array,10);
}
#endif
|