let arr = [9, 8, 7, 6, 2, 3, 4, 5, 1, 10]
function quickSort(arr) {
if (arr.length <= 1) return arr
let pivotIndex = Math.floor(arr.length / 2)
let pivot = arr.splice(pivotIndex, 1)[0]
let [left, right] = [[], []]
for (let i = 0; i < arr.length; i++) {
let item = arr[i]
if (item <= pivot) {
left.push(item)
}
else {
right.push(item)
}
}
return quickSort(left).concat([pivot], quickSort(right))
}
function bubbleSort (arr) {
let len = arr.length
for (let j = 0; j < len - 1; j++) {
for (let i = 0; i < len - 1 - j; i++) {
if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]
}
}
}
return arr
}
function selectSort(arr) {
let len = arr.length
for (let i = 0; i < len - 1; i++) {
let minIndex = i
for (let j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j
}
}
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]
}
return arr
}
function insertSort(arr) {
let len = arr.length
for (let i = 1; i < len; i++) {
let current = arr[i]
let preIndex = i - 1
while (preIndex >= 0 && arr[preIndex] > current) {
arr[preIndex + 1] = arr[preIndex]
preIndex--
}
arr[preIndex + 1] = current
}
return arr
}