void swap(int *a, int *b) { int t=*a; *a=*b; *b=t; } void sort(int arr[], int beg, int end) { if (end > beg + 1) { int piv = arr[beg], l = beg + 1, r = end; while (l < r) { if (arr[l] <= piv) l++; else swap(&arr[l], &arr[--r]); } swap(&arr[--l], &arr[beg]); sort(arr, beg, l); sort(arr, r, end); } } |
// quickSort // // This public-domain C implementation by Darel Rex Finley. // // * Returns YES if sort was successful, or NO if the nested // pivots went too deep, in which case your array will have // been re-ordered, but probably not sorted correctly. // // * This function assumes it is called with valid parameters. // // * Example calls: // quickSort(&myArray[0],5); // sorts elements 0, 1, 2, 3, and 4 // quickSort(&myArray[3],5); // sorts elements 3, 4, 5, 6, and 7 bool quickSort(int *arr, int elements) { #define MAX_LEVELS 1000 int piv, beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R ; beg[0]=0; end[0]=elements; while (i>=0) { L=beg[i]; R=end[i]-1; if (L<R) { piv=arr[L]; if (i==MAX_LEVELS-1) return NO; while (L<R) { while (arr[R]>=piv && L<R) R--; if (L<R) arr[L++]=arr[R]; while (arr[L]<=piv && L<R) L++; if (L<R) arr[R--]=arr[L]; } arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L; } else { i--; }} return YES; } |
piv | [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | ||
? | 4 | 5 | 3 | 6 | 2 | 8 | 1 | 7 | Starting condition. | |
Copy first item to pivot stash. | ||||||||||
4 | 4 | 5 | 3 | 6 | 2 | 8 | 1 | 7 | ||
Set L and R to the span of the partition. | ||||||||||
Decrement R until it finds an item that is less than the pivot value. | ||||||||||
Copy that item to position L. | ||||||||||
4 | 1 | 5 | 3 | 6 | 2 | 8 | 1 | 7 | ||
Increment L until it finds an item that is more than the pivot value. | ||||||||||
Copy that item to position R. | ||||||||||
4 | 1 | 5 | 3 | 6 | 2 | 8 | 5 | 7 | ||
Decrement R until it finds an item that is less than the pivot value. | ||||||||||
Copy that item to position L. | ||||||||||
4 | 1 | 2 | 3 | 6 | 2 | 8 | 5 | 7 | ||
Increment L until it finds an item that is more than the pivot value. | ||||||||||
Copy that item to position R. | ||||||||||
4 | 1 | 2 | 3 | 6 | 6 | 8 | 5 | 7 | ||
Decrement R — now L is not less than R, so... | ||||||||||
...copy the pivot to position L. | ||||||||||
4 | 1 | 2 | 3 | 4 | 6 | 8 | 5 | 7 | Done! | |
piv | [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] |
piv | T | [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | |||
? | ? | 4 | 5 | 3 | 6 | 2 | 8 | 1 | 7 | Starting condition. | ||
Copy first item to pivot stash. | ||||||||||||
4 | ? | 4 | 5 | 3 | 6 | 2 | 8 | 1 | 7 | |||
Initialize L and R. | ||||||||||||
Increment L until it finds an item larger than the pivot. | ||||||||||||
Decrement R. | ||||||||||||
Swap items L and R through swap var T. | ||||||||||||
4 | 5 | 4 | 5 | 3 | 6 | 2 | 8 | 1 | 7 | |||
4 | 5 | 4 | 7 | 3 | 6 | 2 | 8 | 1 | 7 | |||
4 | 5 | 4 | 7 | 3 | 6 | 2 | 8 | 1 | 5 | |||
Increment L until it finds an item larger than the pivot. | ||||||||||||
Decrement R. | ||||||||||||
Swap items L and R through swap var T. | ||||||||||||
4 | 7 | 4 | 7 | 3 | 6 | 2 | 8 | 1 | 5 | |||
4 | 7 | 4 | 1 | 3 | 6 | 2 | 8 | 1 | 5 | |||
4 | 7 | 4 | 1 | 3 | 6 | 2 | 8 | 7 | 5 | |||
Increment L until it finds an item larger than the pivot. | ||||||||||||
Decrement R. | ||||||||||||
Swap items L and R through swap var T. | ||||||||||||
4 | 6 | 4 | 1 | 3 | 6 | 2 | 8 | 7 | 5 | |||
4 | 6 | 4 | 1 | 3 | 8 | 2 | 8 | 7 | 5 | |||
4 | 6 | 4 | 1 | 3 | 8 | 2 | 6 | 7 | 5 | |||
Increment L until it finds an item larger than the pivot. | ||||||||||||
Decrement R. | ||||||||||||
Swap items L and R through swap var T. | ||||||||||||
4 | 8 | 4 | 1 | 3 | 8 | 2 | 6 | 7 | 5 | |||
4 | 8 | 4 | 1 | 3 | 2 | 2 | 6 | 7 | 5 | |||
4 | 8 | 4 | 1 | 3 | 2 | 8 | 6 | 7 | 5 | |||
Increment L — now L is not less than R, so... | ||||||||||||
...decrement L... | ||||||||||||
...and swap item L and the first item through swap var T. | ||||||||||||
4 | 2 | 4 | 1 | 3 | 2 | 8 | 6 | 7 | 5 | |||
4 | 2 | 4 | 1 | 3 | 4 | 8 | 6 | 7 | 5 | |||
4 | 2 | 2 | 1 | 3 | 4 | 8 | 6 | 7 | 5 | Done. | ||
piv | T | [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] |
// quickSort // // This public-domain C implementation by Darel Rex Finley. // // * This function assumes it is called with valid parameters. // // * Example calls: // quickSort(&myArray[0],5); // sorts elements 0, 1, 2, 3, and 4 // quickSort(&myArray[3],5); // sorts elements 3, 4, 5, 6, and 7 void quickSort(int *arr, int elements) { #define MAX_LEVELS 300 int piv, beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R, swap ; beg[0]=0; end[0]=elements; while (i>=0) { L=beg[i]; R=end[i]-1; if (L<R) { piv=arr[L]; while (L<R) { while (arr[R]>=piv && L<R) R--; if (L<R) arr[L++]=arr[R]; while (arr[L]<=piv && L<R) L++; if (L<R) arr[R--]=arr[L]; } arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L; if (end[i]-beg[i]>end[i-1]-beg[i-1]) { swap=beg[i]; beg[i]=beg[i-1]; beg[i-1]=swap; swap=end[i]; end[i]=end[i-1]; end[i-1]=swap; }} else { i--; }}} |