Last active
March 21, 2017 14:22
-
-
Save quanon/eb23f1e92a7b7ef69a1cae0c3858bc6b to your computer and use it in GitHub Desktop.
quicksort
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
const quicksort = (array) => { | |
if (array.length <= 1) return array; | |
const pivot = array[Math.floor(Math.random() * array.length)]; | |
// 余計な走査は許してください。 | |
const left = array.filter(i => i < pivot); | |
const middle = array.filter(i => i === pivot); | |
const right = array.filter(i => i > pivot); | |
console.log({ array, left, middle, right }); | |
// 余計な [] は許してください。 | |
return [].concat(quicksort(left), middle, quicksort(right)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment