Javascript USES the push method of arrays for quicksort

  • 2020-03-30 03:55:07
  • OfStack

There are many ways to sort, and this section describes the use of the push method of arrays for quicksort


function quickSort(arr){
 if(arr.length <= 1) return arr;//Determines if the array is valid
 var cut = Math.floor(arr.length/2);//Take the middle subscript
 var left = [],right = [];
 var num = arr.splice(cut,1)[0];//Take basic value
 for(var i = 0;i < arr.length;i ++){
  if(arr[i] < num){
   left.push(arr[i]);//The smaller one is on the left
  }else {
   right.push(arr[i]);//Put the big one on the right
  }
 }
 return quickSort(left).concat(num,quickSort(right));// recursive 
}

Related articles: