function quickSort(arr>{ //如果陣列只有一個數,就直接回傳;
if(arr.lengthreturn arr;
}
//找到中間的那個數的索引值;如果是浮點數,就向下取整
var centerIndex = Math.floor(arr.length/2);
//根據這個中間的數的索引值,找到這個數的值;
var centerNum = arr.splice(centerIndex,1);
//存放左邊的數
var arrLeft = [];
//存放右邊的數
var arrRight = [];
for (i=0;i
if(arr[i]arrLeft.push(arr[i])
}else if(arr[i] >centerNum){
arrRight.push(arr[i])
}
}
return quickSort(arrLeft).concat(centerNum,quickSort(arrRight));
}; >var arrSort = [33,18,2,40,16,63,27];
var arr1 = quickSort(arrSort);
console.log(arr1);