Created
August 23, 2024 14:47
-
-
Save Hoyasumii/4afe8825b49ede09627176aa38c4761b 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
const data: Array<number> = [4, 20, 9, 74, 23, 100, 1, 5, 91]; | |
function quicksort(data: Array<number>): Array<number> { | |
if (data.length < 2) { | |
return data; | |
} | |
const pivot: number = data[Math.floor(data.length / 2)]; | |
const min: Array<number> = data.filter(item => item < pivot); | |
const max: Array<number> = data.filter(item => item > pivot); | |
return [ ...quicksort(min), pivot, ...quicksort(max) ]; | |
} | |
console.time("data"); | |
console.log(quicksort(data)); | |
console.timeEnd("data"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment