Quick sort

What is quick sort?

The quick sort by C. A. R. Hoare was proposed in 1962.

Its basic idea is that..:

By sorting, the sorted data is split into two separate parts, each of which is smaller than all the other parts

Then, this method is used to sort the two parts respectively, and the whole sorting process can be recursively carried out so that the whole data becomes ordered sequence.

Realization idea:

1. first take a number from the sequence as the base number.

2. partition process, the number of larger than this number to its right, less than or equal to its number, all put it on the left.

3. repeat the second steps to the left and right intervals until there is only one number in each interval.

Process diagram:

Quick sort

As shown above, after the first round of division array with 32 bit boundaries is divided into two parts, the left is smaller than 32, on the right are greater than 32, then simply repeat this procedure for the two partitions, until each partition has a data location.

Code implementation:

/ * *

* @param s

* @param L

* @param R

* /

Static, void, quickSort (int, s[], int, l, int, R) {

If (L, &lt, R) {

Int, I = L, j = R, x = s[l];

While (I, &lt, J) {

While (I < J & & s[j] &gt = = x)

From right to left / / find first number smaller than x


If (I, &lt, J)

S[i++] = s[j];

While (I < J & & X; s[i]; <)

From left to right / find first is greater than or equal to the number of X


If (I, &lt, J)

S[j–] = s[i];


S[i] = x;

QuickSort (s, l, I – 1); / / recursive calls

QuickSort (s, I + 1, R);



Quick sort

Sum up: