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
| #include<stdio.h>
int Partition(int A[], int low, int high) { int pivot = A[low]; while (low < high) { while (low < high && A[high] >= pivot) { high -= 1; } A[low] = A[high]; while (low < high && A[low] <= pivot) { low += 1; } A[high] = A[low]; } A[low] = pivot; return low; }
void QuickSort(int A[], int low, int high) { if (low < high) { int point = Partition(A, low, high); QuickSort(A, low, point - 1); QuickSort(A, point + 1, high); } }
int main() { int A[] = { 52,6,65,52,85,33,99,125,22,33,55,66,55,88,77,55,66,55,22,59 }; QuickSort(A, 0, (sizeof(A) / sizeof(A[0]) - 1)); for (int i = 0; i < (sizeof(A) / sizeof(A[0])); i++) { printf("%d ", A[i]); } return 0; }
|