Last active
February 18, 2020 15:59
-
-
Save JoshuaSkootsky/d15a9251eed30598aceddf104a861359 to your computer and use it in GitHub Desktop.
A functional quicksort in Javascript
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
// a functional quicksort in javascript | |
function qSort(arr) { | |
if (arr.length <= 1) return arr; | |
else { | |
const pivPoint = Math.floor(arr.length / 2); | |
const pivot = arr[pivPoint]; | |
const newArr = [].concat( | |
qSort(arr.filter(x => x < pivot)), | |
arr.filter(x => x === pivot), | |
qSort(arr.filter(x => x > pivot)), | |
); | |
return newArr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment