const quickSort = (array) = > {
const sort = (arr, left = 0, right = arr.length - 1) = > {
if (left >= right) {// If the left index is greater than or equal to the right index, the collation is complete
return
}
let i = left
let j = right
const baseVal = arr[j] // Take the last digit of the unordered array as the reference value
while (i < j) {// Place all numbers smaller than the base value on the left and those larger on the right
while (i < j && arr[i] <= baseVal) { // Find a swap greater than the base value
i++
}
arr[j] = arr[i] If there is no number greater than the base value, you assign yourself to yourself (I equals j).
while (j > i && arr[j] >= baseVal) { // Find a swap smaller than the base value
j--
}
arr[i] = arr[j] If you can't find anything less than the base value, you assign yourself to yourself (I equals j).
}
arr[j] = baseVal // Place the base value in the central position to complete the loop (this time j equals I)
sort(arr, left, j - 1) // Repeat the above operation for the unordered array on the left
sort(arr, j + 1, right) // Repeat the above operation for the unordered array on the right
}
const newArr = array.concat() // To ensure that this function is pure, copy the array once
sort(newArr)
return newArr
}
Copy the code