Created
April 23, 2019 08:16
-
-
Save 4skinSkywalker/5dfa91e0d4789733b9b645929571122b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function pivotHelper(array, start = 0, end = array.length) { | |
let pivotValue = array[start] | |
let pivotIndex = start | |
for (let i = start + 1; i < end; i++) { | |
if (pivotValue >= array[i]) { | |
pivotIndex++ | |
[array[pivotIndex], array[i]] = [array[i], array[pivotIndex]] | |
} | |
} | |
[array[pivotIndex], array[start]] = [array[start], array[pivotIndex]] | |
return pivotIndex | |
} | |
function quickSort(array, left = 0, right = array.length) { | |
if (left === right) { | |
return array | |
} | |
let pivotIndex = pivotHelper(array, left, right) | |
quickSort(array, left, pivotIndex) | |
quickSort(array, pivotIndex + 1, right) | |
return array | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment