The idea of "quick sorting" is very simple, and the entire sorting process only requires three steps:
(1) Find a benchmark in the data set
(2) Create two arrays, storing the left and right arrays respectively
(3) Use recursion to make the next comparison
Look at a demo: http://jsdo.it/norahiko/oxIy/fullscreen (The web page may be slow to open, wait slowly)
<script type="text/javascript"> function quickSort(arr){ if(arr.length<=1){ return arr;//If there is only one number in the array, it will be returned directly; } var num = Math.floor(arr.length/2);//Findex the index value of the intermediate number. If it is a floating point number, round down var numValue = arr.splice(num,1);//Findex the value of the intermediate number var left = []; var right = []; for(var i=0;i<arr.length;i++){ if(arr[i]<numValue){ left.push(arr[i]);//The number on the left of the reference point is passed to the left array} else{ right.push(arr[i]);//The number on the right of the reference point is passed to the right array} } return quickSort(left).concat([numValue],quickSort(right));//Recursively repeat comparison}alert(quickSort([32,45,37,16,2,87]));//Pop "2,16,32,37,45,87"</script>