Quick sorting by Sorting Algorithm

Source: Internet
Author: User

Quick sorting by Sorting Algorithm

The basic idea of quick sorting is to split the data to be sorted into two separate parts by one sort. 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.

Fast sorting is an unstable sorting algorithm. That is to say, the relative positions of multiple identical values may change at the end of the algorithm.

Quick sorting is A sort by Division and exchange proposed by C. R. A. Hoare in 1962. It adopts a sub-Governance Policy, usually called Divide-and-Conquer Method ).

The basic idea of this method is:

1. First, extract a number from the series as the reference number.

2. In the partitioning process, place all the numbers greater than this number to its right, and all the numbers smaller than or equal to it to its left.

3. Repeat the second step for the left and right intervals until each interval has only one number.

Take an array as an example, and take the first number of intervals as the reference number.

0

1

2

3

4

5

6

7

8

9

72

6

57

88

60

42

83

73

48

85

Initially, I = 0; j = 9; X = a [I] = 72

Since the number in a [0] has been saved to X, it can be understood that a pitfall is dug on Array a [0] and other data can be filled here.

Start from j and find a number smaller than or equal to X. When j = 8, the condition is met, dig a [8] and fill it in the previous pit a [0. A [0] = a [8]; I ++; this kind of a [0] is fixed, but a new pitfall a [8] is formed. what should I do? Simple. Fill in a [8] with numbers. This time, start from I and find a number greater than X. When I = 3, the condition is met, dig a [3] and fill in a [8] = a [3]; j --;

The array is changed:

0

1

2

3

4

5

6

7

8

9

48

6

57

88

60

42

83

73

88

85

I = 3; j = 7; X = 72

Repeat the preceding steps, first forward and then forward.

Start from j. When j = 5 and meet the conditions, dig a [5] into the previous pit. a [3] = a [5]; I ++;

Start from I and look back. When I = 5, it exits because I = j.

At this time, I = j = 5, and a [5] is just the last pitfall, so fill X in a [5].

The array is changed:

0

1

2

3

4

5

6

7

8

9

48

6

57

42

60

72

83

73

88

85

It can be seen that the numbers before a [5] are smaller than them, and the numbers after a [5] are greater than them. Therefore, for a [0... 4] and a [6... 9] You can repeat the preceding steps in the two subintervals.

Summarize the number of pitfalls

1. I = L; j = R; dig out the reference number to form the first pit a [I].

2. j -- find a smaller number than it in the back and forward, and dig out the number in the previous pit a [I.

3. I ++ finds a larger number from the front to the back, and then mines the number to fill in the previous pit a [j.

4. Repeat steps 2 and 3 until I = j and enter the baseline number in a [I.

# Include
Using namespace std;
Void quickSort (int a [], int, int );
Int main ()
{
Int array [] = {,}, k;
Int len = sizeof (array)/sizeof (int );
Cout <"The orginal array are:" < For (k = 0; k Cout < QuickSort (array, 0, len-1 );
Cout <"The sorted array are:" < For (k = 0; k Cout < System ("pause ");
Return 0;
}

Void quickSort (int s [], int l, int r)

{

If (l <r)

{

Int I = l, j = r, x = s [l];

While (I <j)

{

While (I <j & s [j]> = x) // find the first number less than x from the right to the left.

J --;

If (I <j)

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

While (I <j & s [I] <x) // from left to right find the first number greater than or equal to x

I ++;

If (I <j)

S [j --] = s [I];

}

S [I] = x;

QuickSort (s, l, I-1); // recursive call

QuickSort (s, I + 1, r );

}

}

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.