Classic Algorithm (6) Quick Sort Quick fix

Source: Internet
Author: User
Tags sort

Quick sort because of the efficiency of sorting efficiency in several sorts of the same O (N*logn), so it is often adopted, coupled with the idea of fast ordering----divide and conquer method is also practical, so many software company's written interview, including such as Tencent, Microsoft and other well-known IT companies like to test this, There are large size of the procedures of the examination, such as soft test, postgraduate grind in also often appear fast sort of figure.

In general, to direct dictation out of a quick sort or a certain degree of difficulty, because I have a quick sort of understanding of the next vernacular interpretation, I hope to help you understand, to achieve a quick sort, quick fix.

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.

2 6
1 4 5 7 8 72 88 60 83 73 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:

2 6
1 4 5 7 8 48 88 60 83 73 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:

2 6
1 4 5 7 8 48 42 60 83 73 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.

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.