#include <cstdlib> #include <iostream> using namespace std; int Partition(int list[], int low, int high) { int pivotkey = list[low]; while(low < high) { while(low<high && list[high]>= pivotkey) high--; //找到第一个小于key的记录 if(low < high) list[low++] = list[high];//相当于交换了list[i]和list[j] while(low<high && list[low]<= pivotkey) low++; if(low < high) list[high--] = list[low]; } list[low] = pivotkey; return low; } void QSort(int list[], int low, int high) { int pivotpos; if(low < high) { pivotpos = Partition(list, low, high); QSort(list, low, pivotpos - 1); QSort(list, pivotpos + 1, high); } } int main(int argc, char *argv[]) { int list[] = {2,52,12,45,86,36,75,33,65,99}; int i; for(i = 0;i < 10;i++) { printf("%5d",list[i]); } printf("\n\n"); QSort(list, 0, 9); //out for(i = 0;i < 10;i++) { printf("%5d",list[i]); } system("PAUSE"); return EXIT_SUCCESS; }