This is basically the same part. [Cpp] void print_arr (int * arr, int n) {int I; for (I = 0; I <n; I ++) {if (! I) {printf ("% d", arr [I]);} else {printf ("% d", arr [I]);} printf ("\ n");} void sort (int * arr, int start, int end) {if (end <start) return; int index = quik_sort (arr, start, end); sort (arr, start, index-1); sort (arr, index + 1, end);} int main () {// cout <"!!! Hello World !!! "<Endl; // prints !!! Hello World !!! Int n = 6; int max = 10; int * arr = (int *) malloc (sizeof (int) * n); srand (time (0 )); // cout <time (0) <endl; for (int I = 0; I <n; I ++) {arr [I] = rand () % max;} print_arr (arr, n); sort (arr, 0, n-1); print_arr (arr, n); return 0;} First Non-randomization algorithm: Method 1: switch from both sides to the middle. [Cpp] int quik_sort (int * arr, int start, int end) {int I = start; int j = end + 1; int x = arr [start]; while (true) {// here is "++ I" while (arr [++ I] <x); // find the first benchmark (x) from the forward) A small number. I point to this number. // -- j, so we need to add j = end + 1 while (arr [-- j]> x) in the front. // the first benchmark (x) from the front to the back) A large number. J points to this number if (I> = j) break; // exchange int temp = arr [I]; arr [I] = arr [j]; arr [j] = temp;} arr [start] = arr [j]; arr [j] = x; // print_arr (arr, 6 ); // cout <start <"-" <end <"j =" <j <endl; return j;} Method 2: First exchange: swap (arr, index, end); is the last value and the last value. (The traversal tree starts from start-> end-1). It can also be said that the arr [end] is saved as the benchmark value, and is exchanged after the loop ends. The second exchange: swap (arr, index ++, I); ensures that smaller values are exchanged in the first three times: swap (arr, end, index); when the current cycle ends, the index points to a certain position in the middle (the value is the position where the benchmark value should be). At this time, arr [index] is greater than or equal to the benchmark value, it can be exchanged with arr [end] (benchmark value). This algorithm can traverse from the front to the back, or traverse from the back to the front. The number indicated by the index can be understood as a number to be exchanged with the following benchmark (the number indicated by I is always smaller than the benchmark and must be switched to the Front ), it can be smaller than the limit (that is, index = I, it is exchanged with itself, index ++) or large (not exchanged, index unchanged ). [Cpp] int quik_sort (int * arr, int start, int end) {int index = start; int begin = arr [index]; swap (arr, index, end ); for (int I = start; I <end; I ++) {if (arr [I] <strong) {swap (arr, index ++, I );}} swap (arr, end, index); return index;} method 3: Only one quick_sort method, recursively calling itself. In fact, it is closer from both sides to the middle. But here we do not exchange two values at a time, but assign one value to another. (The key is always stored with the reference value, that is, a location is empty and can be sorted in the same place .) This is what I think is better than the first two! Because there won't be too many exchange operations. [Cpp] void quick_sort (int * arr, int start, int end) {if (end <start) return; int I, j, key; I = start; j = end; key = arr [I]; while (I <j) {while (I <j & arr [j]> key) j --; if (I <j) arr [I ++] = arr [j]; while (I <j & arr [I] <key) I ++; if (I <j) arr [j --] = arr [I];} arr [I] = key; if (start <i-1) quick_sort (arr, start, I-1 ); if (end> I + 1) quick_sort (arr, I + 1, end);} randomization algorithm: the advantage of randomization is to prevent the worst case. Java version is actually very simple, it is to get a random number. [Java] www.2cto. comprivate static <E> int partition (E [] array, int begin, int end, Comparator <? Super E> cmp) {int index = begin + RND. nextInt (end-begin + 1); E begin = array [index]; swap (array, index, end); for (int I = index = begin; I <end; ++ I) {if (cmp. compare (array [I], blocks) <= 0) {swap (array, index ++, I) ;}} swap (array, index, end ); return (index );}