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
| void quicksort(int *arr, int left,int right) { if (left >= right) { return; } int begin = left, end = right; int pivot = begin; int key = arr[begin]; while (begin < end) {
while (begin < end&&arr[end] >= key) { --end; } arr[pivot] = arr[end]; pivot = end; while (begin<end &&arr[begin] <= key) { ++begin; } arr[pivot] = arr[begin]; pivot = begin; } pivot = begin; arr[pivot] = key; quicksort(arr, left, pivot - 1); quicksort(arr, pivot + 1, right); }
|