A fast sorting _javascript technique based on javascript implementation

Source: Internet
Author: User
function QuickSort (arr) {
    //If the array has only one number, it is returned directly;
    if (arr.length<1) {return
      arr;  
    } 
    Find the index value of the middle number, and if it is a float, take down the whole
    var centerindex = Math.floor (ARR.LENGTH/2);
    The value of this number is found according to the index value of the middle number;
    var centernum = Arr.splice (centerindex,1);
    Store the number on the left
    var arrleft = [];
    Store the right number
    var arrright = [];
    for (i=0;i<arr.length;i++) {
      if (arr[i]<centernum) {
        Arrleft.push (arr[i))
      }else if (arr[i]> Centernum) {
        arrright.push (Arr[i])
      }
    } return
    QuickSort (arrleft). Concat (Centernum,quicksort ( Arrright));    
  };
  var arrsort = [33,18,2,40,16,63,27];
  var arr1 = QuickSort (arrsort);
  Console.log (ARR1);

"Miao-Flavor class" of the first phase of video teaching.

The main principle is: the principle of rapid sorting: Find the datum point, create two arrays to store, and recursively

Datum point: Is to find a number in the middle of this array;

Set up two arrays to store separately: that is, with this datum point, its left and right values are stored in the two defined new array;

Recursion: Calling itself inside a function;

The point I'm summarizing here is when using recursion:

1. Must have a judgment and return a value; otherwise, it's a dead loop.

2. In the internal call itself, the passing of the parameter is an internal definition of a variable, this variable and the initial transmission of parameters, associated;

3. To perform the same work, consideration may be given to the use of recursion;

This is the first time a variable of a function is executed: The median is 40, and according to the judgment condition of the cycle less than 40 is stored in Arrleft, more than 40 of which is stored in arrright. The following figure

Second Call function

, when executed to return QuickSort (Arrleft). Concat (Centernum,quicksort (arrright));

QuickSort (Arrleft) will call the function, and the arguments passed are [33,18,2,16,27]

The median is 2, lower than 2 small left arrleft, 2 larger than the right arrright

Finally go to call Quicksort (Arrright)

The loop calls itself back, until the passed parameter length, less than 1, returns the incoming argument.

The above is the entire content of this article, I hope to help you, thank you for the support of cloud habitat community!

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.