Fast sorting algorithm

Source: Internet
Author: User

The bubble sort in the previous section can be said that we learn the first real sorting algorithm, and solve the problem of bucket sorting waste space, but in the implementation of the algorithm has sacrificed a lot of efficiency, its time complexity reached O (N2). If our computer can run 1 billion times a second, sort 100 million, the bucket sort takes only 0.1 seconds, and the bubble sort takes 10 million seconds to reach 115 days, which is scary. Is there a sort algorithm that doesn't waste space and can be faster? That's "quick sort"! Just listen to the name is not very high-end.

Let's say we're sorting the 9 number of "6 1 2 7 3 4 5 10 8 10" now. First of all, find a number in this sequence as the base number (don't be intimidated by the noun, just a number to refer to, you'll know what it's for later). For convenience, let the first number of 6 be the benchmark. Next, you need to put all the numbers in this sequence that are larger than the datum to the right of 6, and lower the number of the datum to the left of 6, similar to the following arrangement.

3 1 2 5 4 6 9 7 10 8

In the initial state, the number 6 is in the 1th position of the sequence. Our goal is to move 6 to a location in the middle of the sequence, assuming that this position is K. Now we need to look for this k, and with the K-bit as the cutoff point, the number on the left is less than or equal to 6, and the right number is greater than or equal to 6. Think about it, do you have a way to do this?

Let me give you a hint. Recall how bubble sort, by "swapping", is a step-by-step way to get each number back. At this point you can also use the "Exchange" method to achieve the goal. What is the specific way to exchange it in one step? How to Exchange is convenient and save time? Don't look down, take out a pen, and draw on the paper. When I first learned the bubble sort algorithm in high school, I thought bubble sort was a waste of time, and it was obviously unreasonable to compare the two numbers to each other. So I thought of a way, then know that this is the "quick sort", please allow me a little narcissism (^o^).

The method is simple: "Probe" starts at both ends of the initial sequence "6 1 2 7 9 3 4 5 10 8". First from the right to the left to find a number less than 6, and then from left to right to find a number greater than 6, and then exchange them. Here you can use two variables I and J to point to the left and right side of the sequence respectively. We have a nice name for these two variables "Sentinel I" and "Sentinel J". At the beginning, let Sentinel I point to the leftmost (i.e. i=1) of the sequence, pointing to the number 6. Let Sentinel J point to the rightmost (i.e. j=10) of the sequence, pointing to the number 8.

First, The Sentinel J begins to deploy. Since the base number set here is the leftmost number, it is important to have Sentinel J go first (think about why). The Sentry, J, moves to the left (ie j--) one step at a point until he finds a number less than 6 to stop. The Sentinel I then moves to the right (ie i++) one step at a point until a number greater than 6 is found to stop. At last the Sentinel J stopped in front of the number 5, and Sentinel I stopped before the number 7.

Now exchange the values of the elements that Sentinel I and Sentinel J are pointing to. The sequence after the exchange is as follows.

6 1 2 5 9 3 4 7 10 8

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.