[Quick sorting]

Source: Internet
Author: User

Quicksort is an improvement in Bubble sorting. Proposed by C. A. R. Hoare in 1962. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence.

Algorithm process

Set the array to be sorted to a [0]... A [N-1], first randomly select a data (usually the first data) as the key data, and then put all the smaller than it before it, all the numbers larger than it are placed behind it. This process is called a fast sorting. It is worth noting that quick sorting is not a stable sorting algorithm, that is, the relative positions of multiple identical values may change at the end of the algorithm.

  The quick sorting algorithm is as follows:

1) set two variables I, j, sorting start: I = 0, j = N-1;

2) Take the first array element as the key data and assign it to the key, that is, key = A [0];

3) search forward from J, that is, search forward from the back (j = J-1), find the first value less than the key a [J], and exchange with a [I;

4) Search backward from I, that is, search backward from the beginning (I = I + 1), find the first a [I] greater than the key, and exchange it with a [J;

5) Repeat steps 3rd, 4, 5 until I = J; (Step 4 is not found in the program when J = J-1, I = I + 1 until found. When I is found and switched, the position of the J pointer remains unchanged. In addition, when I = J, the process must be exactly the end of the last loop completed by I + or J)

For example, the values of array a to be sorted are: (initial key data: x = 49)Note that the key value x remains unchanged in a row and is always compared with X. No matter what position you are in, the final goal is to put X in the middle and put a small value in front and a large value in the back.

A [0] a [1] A [2] a [3] a [4] a [5] a [6]:

49 38 65 97 76 13 27

After the first exchange: 27 38 65 97 76 13 49

(Start from the end of step 3 of the algorithm)

After the second exchange: 27 38 49 97 76 13 65

(According to the fourth step of the algorithm, find the value> X from the beginning, 65> 49, the two are exchanged, at this time: I = 3)

After the third exchange: 27 38 13 97 76 49 65

(Follow the fifth step of the algorithm to find the third step for executing the algorithm again.

After the fourth exchange: 27 38 13 49 76 97 65

(According to the fourth step of the algorithm, find the value greater than X from the beginning, 97> 49, exchange the two, at this time: I = 4, j = 6)

At this time, we will find that I = J and end the fast sorting. After a fast sorting, the result is: 27 38 13 49 76 97 65, that is to say, all the numbers greater than 49 are behind 49, so all the numbers smaller than 49 are above 49.

Fast sorting is a recursive call to this process-split the data sequence with 49 as the midpoint and perform similar fast sorting on the previous and subsequent parts respectively to complete the fast sorting of all data sequences, finally, the data sequence is converted into an ordered sequence. According to this idea, the entire process of fast sorting for the preceding array a is shown in 6:

Initial status {49 38 65 97 76 13 27}

After a quick sorting, it is divided into {27 38 13} 49 {76 97 65}

Sort the first and second parts respectively. {27 38 13} after switching between step 3 and Step 4, the parts are sorted by {13 27 38.

{76 97 65} after switching between step 3 and Step 4, it is changed to {65 76 97.

It is worth mentioning:

Fast randomization:The worst case of fast sorting is based on the choice of the principal component for each division. Select the first element as the principal element for basic quick sorting. In this way, when the array is ordered, the worst result is obtained for each division. A common optimization method is the randomization algorithm, that isRandomly select an element as the principal component. In this case, although the worst case is O (n ^ 2), the worst case is no longer dependent on input data, but the random function value is not good. In fact, the probability of a rapid randomization sorting to obtain the theoretical worst case is only 1/(2 ^ N ). Therefore, the random fast sorting can be performed on the time complexity of the vast majority of input data reaching O (nlogn. A predecessor made an incisive conclusion: "randomization and fast sorting can satisfy a person's character needs for a lifetime ."

The only drawback of rapid randomization sorting is that, once many identical data are input, the Randomization effect will be reduced directly. For the limit condition, that is, for N identical number sorting, the time complexity of the Randomization fast sorting will undoubtedly be reduced to O (N ^ 2 ). The solution is to use a method for scanning so that the principal component is retained in the original position without switching.

# Include <iostream> using namespace STD; # define n 1000 // ******************* C ++ recursive fast rank (random) * **************** // int A [n]; void quicksort (int * randdata, int L, int R) {int I = L, j = r, key = 0; Key = randdata [(L + r)> 1]; Key = randdata [(RAND () % (R-l + 1) + L]; // generate a random number greater than or equal to L less than or equal to R (if it is not random, take the first element as the key value each time !) Do {While (randdata [J]> key) & (j> L) // number of less than the value in the right scan j --; while (randdata [I] <key) & (I <r) // The number of I ++; if (I <= J) {// switch key value and current value swap (randdata [I], randdata [J]); I ++; j -- ;}} while (I <= J ); // If the subscripts of the two scans are staggered, stop (once completed) if (L <j) {// when the left part has a value (left <j ), recursion left half edge quicksort (randdata, L, J);} If (r> I) {// when the right side has a value (Right> I), recursion Right Half Edge quicksort (randdata, i, R) ;}} int main () {int N; while (scanf ("% d", & N )! = EOF) {int I, j, k; for (I = 0; I <n; I ++) A [I] = rand () % (999999999-0 + 1) + 0; quicksort (A, 0, n-1); for (I = 0; I <n; I ++) printf ("% d", a [I]); puts ("") ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.