JavaScript fast sorting algorithm-basic knowledge

Source: Internet
Author: User

A quick sort is an improvement to the bubble sort. By sorting the data that will be sorted into two separate parts, a part of all the data is smaller than the other part of all the data, and then the two parts of the data for the rapid sequencing, the entire sequencing process can be recursive, and finally reached the entire data into an ordered sequence.

Suppose the array to sort is a[0] ... A[N-1], first select any data (usually the first number of the array) as the datum, and then put all the smaller numbers before it, and all the numbers larger than it are placed behind it, this process is called a quick sort. It is noteworthy that fast sorting is not a stable sort algorithm, i.e., the relative position of multiple identical values may change at the end of the algorithm.
A quick sort of algorithm is:
1 set two variables low, high, sort start: low=0,high=n-1;
2 the first array element as the datum, the value is assigned to base, i.e. base=a[0];
3 from high start to search forward, that is, after starting forward search (high--), find the first value less than base A[high], will A[high] and A[low] interchange;
4 from low start backward search, that is, from the front start backward search (low++), find the first larger than base A[low], will a[low] and A[high] interchange;
5 Repeat the 3rd and 4 steps until Low=high;

Copy Code code as follows:

function partition (elements, low, high) {
Default left first element as Datum element
var base=elements[low];
while (Low < high) {
Search backwards until you find elements that are smaller than the Datum element and Exchange
while (Low < high && Elements[high] >= base) high--;
var Swap1=elements[low];elements[low]=elements[high];elements[high]=swap1;
Search backwards until you find an element that is larger than the datum element and swap
while (Low < high && Elements[low] <= base) low++;
var swap2=elements[low];elements[low]=elements[high];elements[high]=swap2;
}
Returns the position of the datum element as the split position of the sequence
return to Low;
}
function sort (elements, low, high) {
if (LowDividing the sequence into two parts is divided into 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 larger than the split position.
var partitionpos=partition (elements, Low, high);
To continue the recursive ordering of the preceding sequence
Sort (elements, 0, partitionPos-1);
Continue recursive sorting of back sequences
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: Best: O (nlog2n), Worst: O (n^2), average: O (nlog2n).

Complexity of Space: O (nlog2n).

Stability: unstable.

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.