Created
March 18, 2019 03:32
-
-
Save shuboc/19795e88ba38e52f790a9c3969561c70 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
function quickSort(arr) { | |
if (arr.length <= 1) { | |
return arr; | |
} | |
const less = []; | |
const greater = []; | |
const pivot = arr[arr.length - 1]; | |
for (let i = 0; i < arr.length - 1; ++i) { | |
const num = arr[i]; | |
if (num < pivot) { | |
less.push(num); | |
} else { | |
greater.push(num); | |
} | |
} | |
return [...quickSort(less), pivot, ...quickSort(greater)]; | |
} | |
const arr = [9, 4, 1, 6, 7, 3, 8, 2, 5]; | |
quickSort(arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment