1. 程式人生 > >遞歸版快速排序-JS代碼

遞歸版快速排序-JS代碼

turn use 排序 spa bsp class js代碼 遞歸版 concat

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

遞歸版快速排序-JS代碼