Quick Sort (JS version)

Source: Internet
Author: User

The time complexity of fast sorting is: O (n*log2n), compared with other O (N2) sorting algorithm, or more advantageous. The original reference is here, because I am a little bit of code in the original text is not understood, so made a small change.

1. Basic idea: Select one in the first or last element of the array, as the datum element, also called the middle axis. By sorting, let the middle axis divide the array into two parts, some smaller than the middle axis, and a part larger. Then the same sort of two parts are sorted by recursive method.

2. Example:

3.js Code

varArrayquick = [7,9,4,8,2,24,54,12,32,11,2];quick (Arrayquick,0,ARRAYQUICK.LENGTH-1);//The latter two parameters determine the quick ordering of a certain part of the array,functionQuick (List,low,high) {if(low<High ) {        varMiddle = getmiddle (List,low,high);//divides a list array into a splitQuick (list,low,middle-1);//recursive ordering of low-word tablesQuick (List,middle+1,high);//recursive ordering of high-character tables    }}functionGetmiddle (list,low,high) {varTMP = List[low];//the first of the array is a datum element, that is, the middle axis     while(low<High ) {         while(low//looking for lower-than-mid-axis data from high-end to mid-axis{ High--; }        //find lower-than-mid-axis data at high end, then swap locationslist[low]=List[high]; List[high]=tmp;  while(low//looking for data higher than the middle axis from the low-side to the middle axis{ Low++; }        //find data at the lower end that is higher than the middle axis, and then swap positionslist[high]=List[low]; List[low]=tmp; }    returnLow//returns the position of the middle axis} for(vari=0; i<arrayquick.length; i++) {Cc.log (arrayquick[i]);}

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.