Created
April 10, 2017 17:02
-
-
Save YozhEzhi/80a0bee6e48610fdb410ed43b9a18351 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
function quicksort(arr) { | |
if (arr.length < 2) return arr; | |
const pivot = arr[0]; | |
const less = arr.filter(item => item < pivot && item !== pivot); | |
const greater = arr.filter(item => item > pivot && item !== pivot); | |
return [...quicksort(less), pivot, ...quicksort(greater)]; | |
} | |
console.log(quicksort([10, 5, 2, 3])); // [2, 3, 5, 10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment