Last active
November 30, 2015 07:45
-
-
Save CN6033/9611318 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define Element_type int | |
void Swap(Element_type *, Element_type*); | |
/* | |
Unstable sort! | |
*/ | |
void Quick_sort(Element_type array[], int left, int right) | |
{ | |
assert left >= 0; // OK ? | |
assert right >= 0; // OK ? | |
if (left >= right){ | |
return; | |
} | |
Element_type pivot = array[right]; | |
int i = left - 1; | |
int j = right; | |
for (;;) | |
{ | |
while (array[++i] < pivot) {/*if (i == right){break; }*/;} // this could never gonna happen | |
while (array[--j] > pivot) {if (j == left -1 ){ break; }} | |
if (i < j) { | |
Swap(&array[i], &array[j]); | |
} else { | |
break; | |
} | |
} | |
Swap(&array[i], &array[right]); // 升序 | |
Quick_sort(array, left, i - 1); | |
Quick_sort(array, i + 1, right); | |
} | |
void Swap(Element_type *p1, Element_type *p2) | |
{ | |
Element_type tmp = *p1; | |
*p1 = *p2; | |
*p2 = tmp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment