Detailed explanation of the Javascript quick sorting algorithm and the javascript Sorting Algorithm

Source: Internet
Author: User

Detailed explanation of the Javascript quick sorting algorithm and the javascript Sorting Algorithm

Quick sorting is an improvement of Bubble sorting. Data to be sorted is divided into two independent parts by one sort. All the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence.

Assume that the array to be sorted is A [0]... A [N-1], first randomly select A data (usually the first number of arrays) as the benchmark data, and then put all the smaller than it before it, all the numbers larger than it are placed behind it. This process is called a fast sorting. It is worth noting that quick sorting is not a stable sorting algorithm, that is, the relative positions of multiple identical values may change at the end of the algorithm.
The quick sorting algorithm is as follows:
1) set two variables low, high, sorting start: low = 0, high = N-1;
2) use the first array element as the benchmark data and assign it to base, that is, base = A [0];
3) Start from high to search forward, that is, start from the back to search forward (high --), find the first value less than base A [high], swap A [high] And A [low;
4) Start from low and search backward, that is, start from front to back (low ++), find the first A [low] with A base greater than swap A [low] And A [high;
5) Repeat steps 3rd and 4 until low = high;

Copy codeThe Code is as follows:
Function partition (elements, low, high ){
// The first element on the left is used as the reference element by default.
Var base = elements [low];
While (low // Search from the back to the back until elements smaller than the base element are found and exchanged
While (low Var swap1 = elements [low]; elements [low] = elements [high]; elements [high] = swap1;
// Search from the beginning to the end until elements greater than the base element are found and exchanged
While (low Var swap2 = elements [low]; elements [low] = elements [high]; elements [high] = swap2;
}
// Return the position of the reference element as the split position of the sequence.
Return low;
}
Function sort (elements, low, high ){
If (low // Split the sequence into two parts: the front and back sequence of the split position. The former sequence is smaller than the value of the split position, and the latter sequence is greater than the value of the split position.
Var partitionPos = partition (elements, low, high );
// Continue recursive sorting of the pre-Sequence
Sort (elements, 0, partitionPos-1 );
// Continue recursive sorting of the post sequence
Sort (elements, partitionPos + 1, high );
}
}
Var elements = [3, 1, 5, 7, 2, 4, 9, 6, 10, 8];
Console. log ('before: '+ elements );
Sort (elements, 0, elements. length-1 );
Console. log ('after: '+ elements );

Efficiency:

Time Complexity: preferably: O (nlog2n), worst: O (n ^ 2), average: O (nlog2n ).

Space complexity: O (nlog2n ).

Stability: unstable.

Related Article

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.