A detailed explanation of JavaScript fast sorting algorithm

Source: Internet
Author: User
Tags arrays comparison sort

The idea of "quick sort" is simple, and the whole sort process takes only three steps:

(1) in the dataset, find a datum point

(2) Set up two arrays to store the left and right arrays, respectively.

(3) using recursion for the next comparison

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <script type= "Text/javascript" >     function qu Icksort (arr) {  if (arr.length<=1) {    return arr;//if the array has only one number, it returns directly;    }      var num = Math.floor (ARR.LENGTH/2);//Find the index value of the middle number, if it is a float, then the   var numvalue is rounded down = Arr.splice (num,1)//Find the value of the middle number   var left = [];   var right = [];      for (var i=0;i<arr.length;i++) {    if (arr[i]<numvalue) {       left.push (Arr[i]) the number to the left of the datum point to the left array           else{      right.push (Arr[i]);//The number to the right of the datum point to the right array     } &NBSP;&NBSP}  return QuickSort (left). Concat ([Numvalue],quicksort (right);//recursive Repeat comparison} alert (QuickSort ([32,45,37,16,2,87]);//pop-up "2,16,32,37,45,87"    </script>
Related Article

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.