Exchange Sort---Quick sort algorithm (JavaScript edition)

Source: Internet
Author: User

A quick sort is an improvement to the bubbling sort. By dividing the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the whole sort process can be recursive, and finally the whole data becomes ordered sequence.

Suppose the array to be sorted is a[0] ... A[n-1], the first arbitrary selection of data (usually the first number of arrays) as the benchmark data, and then all the smaller than the number of it in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip. It is important to note that fast sorting is not a stable sorting algorithm, that is, 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 and high, when the sort starts: low=0,high=n-1;
2) The first array element as the datum data, assigns the value to base, namely base=a[0];
3) forward search from high, that is, after starting the 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, start backward search (low++), find the first A[low] greater than base], will a[low] and A[high] interchange;
5) Repeat the 3rd and 4 steps until Low=high;

The following code is executed in Nodejs

functionpartition (elements, low, high) {//default to the left first element as the Datum element  varBase=Elements[low];  while(Low <High ) {    //search backward, until you find an element that is smaller than the base element, and swap     while(Low < High && Elements[high] >= base) high--; varswap1=elements[low];elements[low]=elements[high];elements[high]=Swap1; //search from the go, until you find an element larger than the base element, and swap     while(Low < High && Elements[low] <= base) low++; varswap2=elements[low];elements[low]=elements[high];elements[high]=SWAP2; }  //returns the position of the datum element as the split position of the sequence  returnLow ;}functionsort (elements, low, high) {if(low<High ) {    //Divide the sequence into two parts, divided into the pre-and post-position sequence, the first sequence is smaller than the value of the split position, the latter sequence is larger than the value of the split position    varpartitionpos=partition (elements, low, high); //continuation of recursive ordering of previous sequencesSort (elements, 0, partitionPos-1); //continuation of recursive ordering of post sequencesSort (Elements, partitionpos+1, high); }}varelements = [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).

Space complexity: O (nlog2n).

Stability: unstable.

Exchange Sort---Quick sort algorithm (JavaScript edition)

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.