const arr = [2, 3, 5, 4, 8, 6, 9, 1, 7];
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) { //每次循环确定一个值,i代表需要的循环次数 不参与每次的比较
for (let j = 0; j < arr.length - i; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
} i
}
return arr
}
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
[arr[j], arr[i]] = [arr[i], arr[j]]
}
}
}
return arr
}
// https://wiki.jikexueyuan.com/project/easy-learn-algorithm/fast-sort.html
function quickSort(arr, left = 0, right = arr.length - 1) {
if (left < right) {//如果左边的索引大于等于右边的索引说明整理完毕
let i = left, j = right, baseVal = arr[j];
while (i < j) {
while (i < j && arr[i] < baseVal) {
i++;
}
while (i < j && baseVal < arr[j]) {
j--;
}
[arr[i], arr[j]] = [arr[j], arr[i]];
}
arr[j] = baseVal;
quickSort(arr, left, j - 1);
quickSort(arr, j + 1, right);
return arr
}
}
console.log(quickSort(arr))
console