Last active
June 18, 2021 09:34
-
-
Save MiCkEyZzZ/37b58555c8f9642f98f5fdad6729bdff to your computer and use it in GitHub Desktop.
Quicksort
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
function quickSort (array) { | |
if (array.length <= 1) { | |
return array | |
} | |
let pivotIndex = Math.floor(array.length / 2) | |
let pivotElement = array[pivotIndex] | |
let leftArray = [] | |
let rightArray = [] | |
for (let i = 0; i < array.length; i++) { | |
if (i === pivotIndex) continue | |
if (array[i] < pivotElement) { | |
leftArray.push(array[i]) | |
} else { | |
rightArray.push(array[i]) | |
} | |
} | |
return [...quickSort(leftArray), pivotElement, ...quickSort(rightArray)] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment