// 快速排序算法
// 时间复杂度为 O(n logn)
function quickSort(arr) {
if(arr.length < 2){ return arr };
var pivotIndex = Math.floor(arr.length / 2);
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
for(var i = 0; i < arr.length; i++){
if(arr[i] <= pivot){
left.push(arr[i]);
}else{
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot], quickSort(right));
}
function getRandomNumbers(min, max, count) {
count = count || 10;
min = min || 0;
max = max || 10;
var res = [];
var temp;
var cap = max - min;
for (var i = 0; i < count; i++) {
temp = Number((Math.random() * cap + min).toFixed(2));
res.push(temp)
}
return res;
}
console.time()
console.log(quickSort(getRandomNumbers(1, 30, 10)))
console.timeEnd()
网友评论