1 "use strict" 2 var arr1=[11,21,3,4,0]; 3 function qSort(arr){ 4 var mid,left,right,len,i,j,emptyArr,emptyArr2; 5 len = arr.length; 6 left = []; 7 right = []; 8 emptyArr=[]; 9 emptyArr2=[]; 10 j = 0; 11 if (len < 2){ 12 return arr 13 }else{ 14 mid = arr[0]; 15 for (i=1; i<len; i++){ 16 if (arr[i] < mid){ 17 left[j] = arr[i] 18 j++ 19 } 20 } 21 j = 0; 22 for (i=1; i<len; i++){ 23 if (arr[i] >= mid){ 24 right[j] = arr[i] 25 j++ 26 } 27 } 28 emptyArr2[0] = mid; 29 return emptyArr.concat(qSort(left), emptyArr2, qSort(right)) 30 } 31 } 32 33 console.log(qSort(arr1))
原文地址:https://www.cnblogs.com/ryzz/p/9005179.html
时间: 2024-10-29 02:45:41