function swap (arr, a, b) {
var temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
function partition (arr, start, end) {
var _start = start;
var _end = end;
let index = start;
start = start - 1;
end = end + 1;
let base = array[~~((start+end)/2)];
while (index !== end) {
if (arr[index] < base) {
start ++;
swap(arr, index, start);
index ++;
}else if (arr[index] === base) {
index ++;
}else if (arr[index] > base) {
end --;
swap(arr, index, end);
}
}
// console.log(`${_start}-${start}`);
// console.log(`${end}-${_end}`);
if (_start < start) {
partition(array, _start, start);
}
if (end < _end) {
partition(array, end, _end);
}
} let array = [-1, 115, 23, 23, 1, 23, 18, 114,5435,4,65,6,5,765,7,6,87,67,67,5,6,45,4,56,546,54]; partition(array,0,array.length - 1);
console.log(array);
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货