"JavaScript algorithm"---fast sorting method

Source: Internet
Author: User

First, the concept of rapid sorting method

We have a messy array for a quick sort, you can first from an array to take an intermediate value, an array is divided into 2, the left of the array with the median comparison, small on the left side, large on the right. After the comparison is finished again to take the median, again compare the analogy two, thinking 1, take the median, and the intermediate value of the subscript 2, create a left empty array, store less than the median value of data 3, create a right empty array, storage greater than the median value of data 4, recursive termination conditions, if the length of the array is equal to 1 Returns an array of 5, loops the array into 26, recursively

Third, the Code
functionquicksort (arr) {if(arr.length<2){        returnarr; }    varMidindex = arr.length%2 = = 0? ARR.LENGTH/2: (arr.length+1)/2; varMID =Arr[midindex]; varleft = []; varright = [];  for(vari=0;i<arr.length;i++){        if(I! = Midindex && Arr[i] <=mid)        {Left.push (arr[i]); }        if(I! = Midindex && Arr[i] >mid)    {Right.push (Arr[i])}} returnquicksort (left). Concat (mid). Concat (quicksort)}

"JavaScript algorithm"---fast sorting method

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.