Quick sorting (c)

Source: Internet
Author: User

 

Quick Sorting Algorithm

Region name

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 that match 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. The program code is as follows:

Program kuaisu (input, output );
Const n = 10;
Var
S: array [1 .. 10] of integer;
K, l, m: integer;

Procedure qsort (lx, rx: integer );
Var
I, j, t: integer;
Begin
I: lx; j: rx; t: s [I];
Repeat
While (s [J]> T) and (j> I) Do
Begin
K: = k + 1;
J: = J-1
End;
If I <j then
Begin
S [I]: = s [J]; I: = I + 1; L: = L + 1;
While (s [I] <t) and (I <j) Do
Begin
K: = k + 1;
I: = I + 1
End;
If I <j then
Begin
S [j]: = s [I]; j: = J-1; l: = l + 1;
End;
End;
Until I = j;
S [I]: = t; I: = I + 1; j: = J-1; l: = l + 1;
If lx <j then qsort (lx, j );
If I <rx then qsort (I, rx)
End; {process qsort End}

Begin
Writeln ('input 10 integer num :');
For m: = 1 to n do read (s [m]);
K: = 0; l: = 0;
Qsort (l, n );
Writeln ('sorting result :');
For m: = 1 to n do write (s [m]: 4)
End.

<49. Exchange the two. J: = 6>

 

 

# Include "stdio. h"
Void quickSort (int a [], int left, int right)
{
Int I, j, temp;
I = left;
J = right;
Temp = a [left];
If (left> right)
Return;
While (I! = J)/* Find the final position */
{
While (a [j]> = temp & j> I)
J --;
If (j> I)
A [I ++] = a [j];
While (a [I] <= temp & j> I)
I ++;
If (j> I)
A [j --] = a [I];

}
A [I] = temp;
Quicksort (A, left, I-1);/* recursion left */
Quicksort (A, I + 1, right);/* Right of recursion */
}

Void main ()
{
Int A [7] = {, 5 };
Int I;
Quicksort (A, 0, 6 );
/* Sort the ordered results */
For (I = 0; I <7; I ++)
Printf ("% 4D", a [I]);
}

 

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.