Quick sort of JavaScript algorithm series (Quicksort)

Source: Internet
Author: User

Quick sort of JavaScript algorithm series (Quicksort)

Original from:

http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/

Https://gist.github.com/paullewis/1981455#file-gistfile1-js

Quick Sort (Quicksort) is a kind of improvement of bubble sort, it is a style of merging and sorting algorithm.

The core idea is to divide the sorted data into two separate parts, one of which is smaller than the rest of the data, and then the two parts of the data are quickly sorted by this method, and the whole sort process can be recursive to achieve the whole data into ordered sequence.

The theoretical steps:

    1. Find a "pivot" item in the array, which can be a center point, datum
    2. The first entry in the array starts the pointer (left pointer).
    3. The last item in the array starts with a pointer (right pointer).
    4. The value in the left pointer array is less than the pivot value, and the left pointer is to the right (plus 1). Continue until the value in the left pointer is greater than or equal to the pivot value.
    5. While the value in the appropriate pointer array is greater than the pivot value, the right pointer is left (minus 1). Continue until the value in the correct pointer is less than or equal to the pivot value.
    6. If the left pointer is less than or equal to the right pointer, then the value of the interchange is in these positions in the array.
    7. Move the left pointer to the right by 1 and one of the right pointers to the left.
    8. If the left and right pointers do not match, go to step 1.

The text is too wordy, simple to say

    1. In the dataset, select an element as the Datum (pivot).
    2. All elements that are smaller than the datum are moved to the left of the datum, and all elements that are larger than the datum are moved to the right of the datum.
    3. For the two subsets to the left and right of the Datum, repeat the first and second steps until only one element is left in all the subsets.

Here's a real demo.

var items = [4, 2, 6, 5, 3, 9];

The specific process algorithm is as follows:

For this set of arrays, how to choose this fulcrum, some algorithms choose the first point for Fulcrum, this is not the best choice, the performance is the worst. Generally better to select the pivot point in the middle of the array, so consider 5 is the pivot value (the length of the array divided by 2)

Next, take the 0 position from the left. The first number is compared with the Fulcrum 5, if 4<5, then the position of the pointer is shifted to 1, then 2<5, and an analogy

If 6>5, the left hand stops moving.

Then move from the pointer on the right, and the same is true, but the right side is taken greater than the value, such as 9>5, forward shift, 3<5, this time also stop moving, and then exchange the pointer corresponding value

First step: Select Pivot point

Second step: The pointer starts before and after the offset

Step three: If 4<5, continue to move the left pointer down

Fourth step: If 2<5, down, 6>5 stop

Fifth step: 9>5, forward, 3<5 stop

Sixth step: The value that the Exchange pointer points to

Seventh Step: Continue the operation as above until the fulcrum

Eighth: If the pointer is to the fulcrum, stop

Code for swap swap with the implementation

function Swap (items, FirstIndex, Secondindex) {    var temp = Items[firstindex];    Items[firstindex] = Items[secondindex];    Items[secondindex] = temp;}    

Code of Implementation

function partition (items, left, right) {var pivot = items[math.f       Loor (right + left)/2 Left, J = right; while (i <= J) {while (Items[i] < pivot) {I++while (Items[j] > pivot) {J--; } if (i <= J) {Swap (items, I, j); I++; J--return I;}        

This function accepts three parameters: this is the array in which the items values are sorted, left which is the exponent to start the left pointer when, and right , this is the exponent to start the right pointer. The pivot value is determined by the left sum right of the values that are then divided by 2. Because this value may be a floating-point number, some rounding is necessary.

The whole algorithm is looping just a loop. The outer ring determines when all array-scoped items have been processed. The left and right pointers have two internal loops to control movement. When two internal loops are completed, the pointer is compared to determine whether the interchange is necessary. After the exchange, two pointers are moved so that the outer loop continues in the right place. The function returns the value of the left pointer as it is used to determine the next time from where the compartment begins. Keep in mind that the partition is happening at the place without generating any extra arrays.

The fast sort algorithm basically divides the entire array by dividing its working principle, and then recursively splits the left and right portions of the array until the entire array is sorted.

In the previous example, the array becomes [4, 2, 3, 5, 6, 9] a partition and returns 4 after the index (the last seat of the left pointer), starting with the recursive left and right 2 segmented arrays

As shown in the figure below

The first step: find the position of the pointer traverse, determine the fulcrum

Step two: Start from left and right hands, compare 4<3, stop

Step three: Because 5>3, move the pointer to the right, because 3==3, stop

Fourth step: The value that the Exchange pointer points to

Fifth step: To deal with it sequentially

Sixth step: Because 2<3, move the left hand, because 4>3, stop

Because the left hand pointer is over the right pointer, stop

After the process, the array becomes [3, 2, 4, 5, 6, 9] and returns an index of 1, which continues to sort until all the arrays are left. Then the same processing proceeds to the right of the array. A quick sort of basic logarithm, then becomes very simple:

 function QuickSort (items, left, right) {var index; if (Items.length > 1 partition (items, left, right); if (Left < Index-1); } if (Index < right) {QuickSort (items, index, right);}} return items;} // first Callvar result = QuickSort (items, 0, items.length-1);            

Fast sequencing is often considered to be efficient and fast, and is characterized by Array.prototype.sort() an array of more than 23 items using the V8 engine implementation. For fewer than 23 items, the V8 uses the insertion sort method [2].

Merge sort is a quick sort of competitor because it is also efficient, fast, but has the advantage of being stable. That's why Mozilla and Safari use it for their own executionArray.prototype.sort()

Quick sort of JavaScript algorithm series (Quicksort)

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.