Skip to content

Instantly share code, notes, and snippets.

@JoshuaSkootsky
Last active February 18, 2020 15:59
Show Gist options
  • Save JoshuaSkootsky/d15a9251eed30598aceddf104a861359 to your computer and use it in GitHub Desktop.
Save JoshuaSkootsky/d15a9251eed30598aceddf104a861359 to your computer and use it in GitHub Desktop.
A functional quicksort in Javascript
// 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