Implementation and principle of quicksort Algorithm

Source: Internet
Author: User

Quick sorting is an improvement of Bubble sorting. Its basic idea is: Split the data to be sorted into two independent parts by means of a lie-down sorting, and all the data in one part is smaller than all the data in the other part, then, the data is sorted by the second method. The whole sorting process can be recursive to convert the entire data into an ordered sequence.

Assume that the array to be sorted is a [1]... A [n], first select a data (usually the first data) as the key data, and then put all the numbers smaller than it in front of it, all the numbers that are larger than it are placed behind it. This process is called "One lie" and "Fast sorting. The following algorithm is used to sort data quickly:

1) set two variables, I: = 1, J: = N;

2) use the first array element as the key data and assign it to X, that is, X: = A [1];

3) Start from J to search forward, that is, start from the back to search forward (J: = J-1), find the first value less than X, the two exchange;

4) Search backward from I, that is, search backward from the beginning (I: = I + 1), find the first value greater than X, and exchange the two;

5) Repeat steps 3rd and 4 until I = J;

For example, the values of array a to be sorted are: (initial key data X: = 49)

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

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, and exchange the two. 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, the two exchange, at this time J: = 4)

At this time, when the third day is not executed, I = J is found, and then 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}

Fast sorting of the first and second parts {13} 27 {38}

End ended {49 65} 76 {97}

49 {65} ended

End

Figure 6 whole process of fast sorting

1) The number of N (assuming n = 10) is set and stored in the S array;

2) In s [1 .. N]. Take an element as the benchmark. For example, t = s [1] is used to determine the position K where T should be in the sorting result. The position of this K is: s [1 .. K-1] <= s [k] <= s [k + 1 .. n], that is, the number before S [k] is smaller than s [K], and the number after s [k] is greater than s [k];

3) The use of the sub-governance idea (that is, the big strategy and small Strategy) can be further applied to s [1 .. K-1] And s [k + 1 .. N] the two groups of data are sorted quickly until the group object has only one data.

If the specific data is as follows, the first process of fast sorting is:

Array Subscript: 1 2 3 4 5 6 7 8 9 10

45 36 18 53 72 30 48 93 15 36

I j

(1) 36 36 18 53 72 30 48 93 15 45

(2) 36 36 18 45 72 30 48 93 15 53

(3) 36 36 18 15 72 30 48 93 45 53

(4) 36 36 18 15 45 30 48 93 72 53

(5) 36 36 18 15 30 45 48 93 72 53

Put 45 in the correct position K in a descending order. Here K is 6, then we apply to s [1 .. 5] and S [6 .. 10] respectively.

In general, the bubble method is the first sorting method that programmers are familiar with. It has the advantages of simple principle and easy programming implementation, but its disadvantage is that the process is too slow. Next I will introduce a simple sorting method that is easy to understand but programming implementation. I don't know if it is the fastest in the existing sorting method, but it is the fastest I have ever seen. Sort the same array. The time required is about 4% of the bubble method. I call it "quick sorting" for the moment ".

The "quick sorting" method uses the recursive principle. The following uses an example to illustrate the principle of "quick sorting. First, an array {, 63,} is given. First, locate the first number -- 53 and use it as the center value. That is to say, 53 should be placed in one position, the value on the left is smaller than it, and the value on the right is greater than it. {, 32, 80, 98}. In this way, the sorting of an array becomes the sorting of two small arrays-the array on the left of 53 and the array on the Right of 53, the two arrays continue in the same way until the order is completely correct.

It doesn't matter if you are not very arrogant. I will provide the following two functions:

/*
N is the array to be sorted. Left and right are the left and right bounds to be sorted,
If you want to sort the preceding array, left and right are 0 and 9 respectively.
*/

Void quicksort (int n [], int left, int right)
{
Int DP;
If (left <right ){

/*
This is the function to be discussed below. According to the above, all the numbers smaller than 53 are put
To its left, place the big one on the right, and return the position of 53 in the sorted array.
*/
Dp = partition (n, left, right );

Quicksort (n, left, dp-1 );

Quicksort (n, DP + 1, right); // these two are recursive calls, respectively sorting the array on the left of 53 and the array on the right
}
}

As we mentioned above, first locate the first number, then sort out the array, put the number smaller than this number to its left, put the big to the right, and then

Returns the position of the median value. The following function is used for this purpose.
Int partition (int n [], int left, int right)
{
Int Lo, hi, salesman, T;

Rows = N [left];
Lo = left-1;
Hi = right + 1;

While (Lo + 1! = Hi ){
If (N [lo + 1] <= then)
Lo ++;
Else if (N [hi-1]> else)
Hi --;
Else {
T = n [lo + 1];
N [++ lo] = N [hi-1];
N [-- Hi] = T;
}
}

N [left] = N [lo];
N [lo] = lo;
Return lo;
}

This program is not difficult and should be very easy to understand. I will give a rough introduction to the process. First, an array and three pointers appear in your mind. The first pointer is called a p pointer, before the end of the entire process, it points to the first number. The second pointer and the third pointer are respectively the lo pointer and the HI pointer, respectively pointing to the leftmost value and the rightmost value. The lo pointer and the HI pointer approach from both sides to the middle at the same time. During the approximation process, they constantly compare with the p pointer value. If the lo pointer value is smaller than the P pointer value, lo ++, still less ++, and then smaller ++ until a value greater than the P pointer is reached, the line of sight is transferred to the HI pointer, if the value of the HI pointer is greater than the value of the P pointer, the value of hi --, the value of the big --, and the value of the big -- until a value smaller than the value of the P pointer is encountered. In this case, the lo pointer value and the HI pointer value are exchanged. This process continues until the two pointers are met. At this time, the p pointer value and the Met value are exchanged, and a new position of the P pointer is returned.

  1. # Include <iostream>
  2. Using namespace STD;
  3. Int A [200001], N;
  4. Void swap (Int & A, Int & B ){
  5. Int TMP =;
  6. A = B;
  7. B = TMP;
  8. }
  9. Int partition (int p, int R ){
  10. Int RND = rand () % (r-p + 1) + P;
  11. Swap (A [RND],

    );

  12. Int PVT = r, I = p-1;
  13. For (Int J = I + 1; j <r; j ++)
  14. If (A [J] <A [PVT])
  15. Swap (A [J], a [++ I]);
  16. Swap (A [++ I], a [PVT]);
  17. Return I;
  18. }
  19. Void qsort (int p, int R ){
  20. If (P <r ){
  21. Int q = partition (P, R );
  22. Qsort (p, q-1 );
  23. Qsort (q + 1, R );
  24. }
  25. }
  26. Int main (){
  27. Cin> N;
  28. For (INT I = 0; I <n; I ++)
  29. Cin> A [I];
  30. Qsort (0, n-1 );
  31. For (INT I = 0; I <n; I ++)
  32. Cout <A [I];
  33. Return 0;
  34. }

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.