Created
July 18, 2018 03:05
-
-
Save moondef/7c733a78d80e7c0c5e82c3baf8826fc2 to your computer and use it in GitHub Desktop.
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 arr = [1, 4, 2, 5, 200, 6, 9] | |
const qsort = (arr) => { | |
if (arr.length < 2) { | |
return arr | |
} else { | |
const pivot = arr[Math.floor(Math.random() * arr.length)] | |
const less = arr.filter(value => value < pivot) | |
const greater = arr.filter(value => value > pivot) | |
return [...qsort(less), pivot, ...qsort(greater)] | |
} | |
} | |
console.log(qsort(arr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment