怎么判断数组是否按照顺序排列 js
发布网友
发布时间:2023-10-21 07:44
我来回答
共1个回答
热心网友
时间:2024-12-05 12:13
var arr = [1,4,2,5,7];
function QuickSort(arr){
if (arr.length <= 1){
return arr;
} else if (arr.length === 2) {
if (arr[0] <= arr[1]) return arr;
else return [arr[1], arr[0]];
} else {
var rndIndex = (Math.random() * arr.length) | 0;
var mid = arr[rndIndex];
arr.splice(rndIndex, 1);
var left = [], right = [];
for (var i=arr.length-1; i>-1; i--) {
if (arr[i] <= mid) left.push(arr[i]);
else right.push(arr[i]);
}
return QuickSort(left).concat(mid, QuickSort(right));
}
}
alert(QuickSort(arr));