Last active
March 25, 2022 06:43
-
-
Save Neo42/9ed62ca3f4adc1dda1748b4a0ecd9568 to your computer and use it in GitHub Desktop.
Lomuto's quick sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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