Created
October 2, 2017 05:54
-
-
Save thebopshoobop/1832f6c9af5db4515b6484bcd5878b91 to your computer and use it in GitHub Desktop.
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 quickSort = arr => { | |
if (arr.length < 2) return arr; | |
const pivot = arr[Math.floor(Math.random() * arr.length)]; | |
let left = []; | |
let equal = []; | |
let right = []; | |
for (let element of arr) { | |
if (element > pivot) right.push(element); | |
else if (element < pivot) left.push(element); | |
else equal.push(element); | |
} | |
return quickSort(left) | |
.concat(equal) | |
.concat(quickSort(right)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment