Skip to content

Instantly share code, notes, and snippets.

@Neo42
Last active March 25, 2022 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Neo42/9ed62ca3f4adc1dda1748b4a0ecd9568 to your computer and use it in GitHub Desktop.
Save Neo42/9ed62ca3f4adc1dda1748b4a0ecd9568 to your computer and use it in GitHub Desktop.
Lomuto's quick sort
const quickSort = (arr, low = 0, high = arr.length - 1) => {
if (low < high) {
const p = partition(arr, low, high)
quickSort(arr, low, p - 1)
quickSort(arr, p + 1, high)
}
}
const partition = (arr, low, high) => {
const pivot = arr[high]
let i = low
for (let j = low; j <= high; j++) {
if (arr[j] < pivot) {
;[arr[i], arr[j]] = [arr[j], arr[i]]
i++
}
}
;[arr[i], arr[high]] = [arr[high], arr[i]]
return i
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment