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 50 51 52 53 54 55 56 57
| #include <stdio.h> #include <stdlib.h>
int AjustArray(int arr[], int l, int r) { int k, n, first;
k = l; n = r; first = arr[k];
while(k < n) { while(k < n && arr[n] < first) { n--; } if(k < n) { arr[k] = arr[n]; k++; }
while(k < n && arr[k] >= first) { k++; } if(k < n) { arr[n] = arr[k]; n--; } }
arr[k] = first; return k; }
void QuickSort(int arr[], int l, int r) { if(l < r) { int i = AjustArray(arr, l, r); QuickSort(arr, l, i-1); QuickSort(arr, i+1, r); } }
void main() { int arr[] = {34, 4, 65, 45, 12, 36, 2, 37, 76, 23}; int len = sizeof(arr)/sizeof(int); QuickSort(arr, 0, len-1);
int i; i= 0;
for(i = 0; i < len; i++) { printf("%d \n", arr[i]); } }
|