quickSearch(arr, left = 0, right = arr.length - 1) {
if (left >= right) {
return;
}
let i = left;
let j = right;
let temp = arr[j];
while (i < j) {
while (i < j && arr[i] <= temp) {
i++;
}
arr[j] = arr[i];
while (i < j && arr[j] >= temp) {
j--;
}
arr[i] = arr[j];
}
arr[j] = temp;
this.quickSearch(arr, left, j - 1);
this.quickSearch(arr, j + 1, right);
},
试一下效果:
let arr = [9, 2, 5, 7, 8, 1, 9, 0, 0, 1];
this.quickSearch(arr);
console.log(arr);
成功排序