JavaScript enables fast ordering of arrays using recursion

Source: Internet
Author: User

//Define a quick Sort methodfunctionQuickSort (arr) {
//set the termination criteria for recursion if(Arr.length <= 1){ returnarr; } //gets the values of the intermediate and intermediate indexes of the array arr varMiddlenum = Math.floor (ARR.LENGTH/2);varMiddlevalue = Arr.splice (Middlenum, 1); //defines the array, left to hold the number less than the middle value, right to hold the number greater than the middle value varleft = []; varright = []; //iterates through the ARR array and compares each item to the median value, in the left and right arrays, respectively, according to the criteria for(vari = 0; i < arr.length; i++ ){ if(Arr[i] <middlevalue) {Left.push (arr[i]); } Else{Right.push (arr[i]); } } //the left and right arrays are compared recursively, until a recursive termination condition is reached, then the final left, Middlenum, and right are combined and returned returnQuickSort (left). Concat (Middlevalue, QuickSort);}//Testvararr = [3,20,16,12,40,32,1,60,55,30];console.log (QuickSort (arr)); //results: [1, 3, A, (+), (+), +, +, +)

JavaScript enables fast ordering of arrays using recursion

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.