The principle of fast sorting algorithm and its JS implementation

Source: Internet
Author: User

To say the principle of fast-line, popular point is to put a thing, divided into a lot of small things to deal with, divided the idea of treatment.

Suppose we now sort the 9 numbers for "6 1 2 7 3 4 5 10 8 10". First of all, in this sequence, look for a number as the base number (do not be frightened by this noun, is a reference to the number, you will know what it is used to do). The first number 6 is generally selected as the base count. Next, you need to put all the numbers in this sequence that are larger than the base number on the right side of 6, the number smaller than the base number 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 digit of the sequence. Our goal is to move 6 somewhere in the middle of the sequence, assuming that this position is K. Now you 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, the right number is greater than or equal to 6.

The ① starts with "probing" from the initial sequence "6 1 2 7 9 3 4 5 10 8" at each end. Find a number that is less than 6 from right to left, then find a number greater than 6 from left to right, then swap them. Here you can use two variables I and J, pointing to the leftmost and rightmost of the sequence, respectively. At first let I point to the leftmost part of the sequence (ie, i=1), pointing to the number 6. Let J point to the far right of the sequence (that is, =10), pointing to the number 8.

② First J started out. Since the number of datums set here is the leftmost number, it is important to have Sentinel J go first. J Move to the left (that is, j--), until a number less than 6 is found to stop . Next I move to the right (that is, i++), until we find a number greater than 6 to stop. Finally J stopped at the number 5, I stopped in front of the number 7.

6 1 2 "7" 9 3 4 "5" 10 8

③ now swaps the values of the elements that I and J point to. The sequence after the interchange is as follows:

6 1 2 "5" 9 3 4 "7" 10 8

To this, the first exchange ends.

④ Next Start J continue to move to the left (again a reminder, each must be J first departure). He found 4 (smaller than the base number 6, met the requirements) and stopped. I also continued to move to the right, and he found 9 (larger than the base number 6 to meet the requirements) and then stopped. At this point the interchange is exchanged again, followed by the following sequence:

6 1 2 5 "4" 3 "9" 7 10 8

The second exchange ends, "probing" continues.

⑤j continued to move to the left, and he found 3 (smaller than the base 6 to meet the requirements) and then stopped. I continue to move to the right, bad! At this point Sentinel I and Sentinel J met, Sentinel I and Sentinel J all walked to 3. Description the "Probe" ends at this point. We swapped the benchmark number 6 and 3. The sequence after the interchange is as follows:

"3" 1 2 5 4 "6" 9 7 10 8

To this first round of "probing" really ended. At this point the base number 6 is the cutoff point, the number of the left 6 is less than or equal to 6, 6 to the right of the number is greater than or equal to 6. Looking back at the process just now, J's mission is to find a number less than the base number, and I's mission is to find a number larger than the base number, until I and J meet.

OK, the explanation is complete. The base number 6 is now returned to the 6th position in the sequence. At this point we have split the original sequence into two sequences with 6 as the dividing point, the sequence on the left is "3 1 2 5 4" and the right sequence is "9 7 10 8". The next step is to process the two sequences separately. Because the 6 left and right sequences are still very confusing at the moment. But it does not matter, we have mastered the method, the next use of recursive thinking, the same way to deal with the 6 left and right of the sequence.

Will eventually get such a sequence, as follows

1 2 3 4 5 6 7 8 9 10

To this, the sort ends completely. Careful classmates may have found that the quick sort of every round of processing is actually the number of the base of the round to the position, until all the numbers are returned, the sorting is over. Below the previous domineering diagram to describe the whole process of the algorithm.

Fast sorting is faster because each exchange is a jump-through compared to the bubble sort. Set a datum point each time it is sorted, place a number less than or equal to the datum point to the left of the Datum point, and place the number greater than or equal to the datum point to the right of the datum point. This will not be the same as the bubble in each exchange of the same time can only be exchanged between the adjacent number, the exchange distance is much larger. Therefore, the total comparison and the number of exchanges is less, the speed naturally increased. Of course, in the worst case, it is still possible to exchange two consecutive numbers. So the worst time complexity for fast sorting is the same as the bubble sort is O (N2), which has an average time complexity of O (Nlogn).

Below through JS to achieve the following quick row.

JS does not need pointers, as long as the size is divided into two arrays of good.

Roughly three steps:

1, to find the benchmark (usually the first item as the benchmark)

2, traversal of the array, less than the base of the left, greater than the benchmark placed in right

3. Recursion

The principle of fast sorting algorithm and its JS implementation

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.