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 41 42 43 44 45 46 47 48 49
| #include <stdio.h>
void EXCHANGE(int *left, int *right) { if (left == right) return; *left ^= *right; *right ^= *left; *left ^= *right; }
int PARTITION(int A[], int left, int right) { int pivotValue = A[right]; int pivotDest = left; for (int i = left; i < right; i++) { if (A[i] <= pivotValue) { EXCHANGE(&A[pivotDest], &A[i]); pivotDest++; } } EXCHANGE(&A[pivotDest], &A[right]); return pivotDest; }
void QUICKSORT(int A[], int left, int right) { if (left < right) { int pivot = PARTITION(A, left, right); QUICKSORT(A, left, pivot - 1); QUICKSORT(A, pivot + 1, right); } }
int main(int argc, char const *argv[]) { int A[] = {7, 3, 9, 5, 8, 4, 2}; QUICKSORT(A, 0, 7); for (int i = 0; i <= 7; i++) { printf("%d ", A[i]); } printf("\n"); return 0; }
|