var quickSort = function(arr) {
console.time(‘2.快速排序耗时‘);
if (arr.length <= 1) { return arr; }
var pivot = arr.splice(0, 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]);
}
}
console.timeEnd(‘2.快速排序耗时‘);
return quickSort(left).concat([pivot], quickSort(right));
};
var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
console.log(quickSort(arr));//[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
时间: 2024-11-05 11:35:00