Quick Sort Details __ Quick sort

Source: Internet
Author: User

The fast sort is a sort of division Exchange C.r.a.hoare in 1962. It adopts a strategy of divide and conquer, which is often referred to as the Partition method (Divide-and-conquermethod).

The basic idea of the method is:

1. First, take a number out of the series as the base number.

2. The partitioning process, which places the number larger than this to its right, is less than or equal to its number on the left.

3. Repeat the second step to the left and right intervals until the interval is only one number.

Although a quick sort is called a divide-and-conquer method, the three-word divide is clearly not a good way to generalize all the steps of a quick sort. So my quick sort is further explained: digging pit filling number + partition method:

Let's take a look at the example, and give the definition below (it's best to summarize the definition in your own words, which is helpful for implementing the code).

Taking an array as an example, the first number of intervals is the base number.

0

1

2

3

4

5

6

7

8

9

72

6

57

88

60

42

83

73

48

85

At the beginning, 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 hole has been dug in the array a[0] and other data can be populated here.

Start with J to find a number that is smaller or equal to x than X. When the j=8, meet the conditions, will a[8] dug and then fill in the last pit a[0]. A[0]=A[8];  i++; Such a pit a[0] was taken care of, but formed a new pit a[8]. Simple, then find the number to fill a[8] this pit. This time from I start looking backwards for a number greater than X, when i=3, in line with the conditions, will a[3] dug again fill in the last pit a[8]=a[3]; j--;

Array into:

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 above steps, looking forward from behind, and looking backwards in the past.

Start looking forward from J, when j=5, meet the conditions, will a[5] dug into the last pit, a[3] = a[5]; i++;

Start looking backwards from I, when i=5, as I==j exits.

At this point, I = j = 5, and a[5] happens to be the last pit dug, so the x is filled in a[5].

Array into:

0

1

2

3

4

5

6

7

8

9

48

6

57

42

60

72

83

73

88

85

You can see that the number in front of a[5] is smaller than it, and the number after a[5] is greater than it . So again to a[0 ... 4] and a[6 ... 9] These two sub ranges repeat the above steps.

Summarize the number of digging pits

1. I =l; j = R; Dig up the base number to form the first pit a[i].

2. j--from behind to find the number of smaller than it, found after digging this number to fill a pit a[i].

3. i++ from the front to find a larger number than it, found also dug this number to fill in the previous pit a[j].

4. Repeat 2, 32 steps until I==j, and fill in a[i].

Following this summary it is easy to realize the code for digging a hole to fill a number:[CPP]  View Plain  copy Int adjustarray (int s[], int l, int r)  // Returns the position of the adjusted datum number    {       int i = l, j = r;        int x = s[l]; //s[l] S[i] is the first pit         while  (i < j)        {            //  find the number less than X from right to left to fill s[i]            while (i < j && s[j] >= x)                  j--;              if (i < j)              {               s[i]  = s[j]; //will S[jFill in s[i], S[j] Creates a new pit                i++;            }               //  find the number greater than or equal to x from left to right to fill s[j]            while (i < j && s[i] < x)                 i++;              if (i < j)             {                s[j] = s[i];  //S[i] into s[j], S[i] formed a new pit                 j--;           }        }   &NWhen bsp;   //exits, I equals J. Fill in this hole with X.        s[i] = x;          return  i;  }  

Re-write the rule code: [CPP]   View plain  copy Void quick_sort1 (int s[], int l ,  int 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.