[Algorithm research]の fast sorting algorithm--javascript implementation

Source: Internet
Author: User

Quick Sort (Quicksort) is the Bubble Sort an improvement.

Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data will be sorted into two separate parts, one part of all the data is smaller than the other part of all the data , and then this method to the two parts of the data to be quickly sorted, the entire sorting process can be recursive To achieve an orderly sequence of the entire data.

The worst case of time complexity is O (NLOGN), a sort that is more commonly used in similar complexity


var s = [72 ,6, 57, 88, 60, 42, 83 ,73, 48,57,72,  85];quicksort (s,0, s.length-1) console.log ("----------------------------") Console.log (S.join ("    ") Function quicksort (s,from,to) {    if (from>=to ) {         return;    }    //Select the starting number as the benchmark      var  i=from,_mid=s[i],j= to;    while (I&LT;J) {         //first from the right to the left looking for a smaller         while than the benchmark (s[j ]&GT;=_MID&NBSP;&NBSP;&AMP;&AMP;&NBSP;I&LT;J) {             j--;        }         if (I&LT;J) {//description found smaller than base, swap             var  temp=s[j];            s[j]=s[i];             s[i]=temp;             i++;        }        // From the left to the right, looking for a larger swap position than the base         while (s[i]<_mid && i &LT;J) {            i++;         }        if (I&LT;J) {//description found larger than base, Exchange              var temp=s[j];             s[j]=s[i];             s[i]=temp;            j--;         }    }    //program jump out of the loop here is the IJ meet   and for the central point      //next to the left and right order (in addition to the baseline number)     quicksort (s,from,i-1);     QuickSort (s,i+1,to);}


Similar to the previous bubble algorithm, and then the last sort of schematic description:

Reprint please specify the Source: http://my.oschina.net/freddon/blog/527809.


[Algorithm research]の fast sorting algorithm--javascript 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.