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> |