Skip to content

Instantly share code, notes, and snippets.

@dengjonathan
Created August 23, 2016 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dengjonathan/32857c21253fd3013fee9e221f4fc1c3 to your computer and use it in GitHub Desktop.
Save dengjonathan/32857c21253fd3013fee9e221f4fc1c3 to your computer and use it in GitHub Desktop.
// implementation of quicksort
var quickSort = function (arr) {
if (arr.length === 0) {
return [];
}
var left = [];
var right = [];
var pivot = arr[arr.length - 1]; // use right most value as pivot
_.each(arr.slice(0, arr.length - 1), function (each) {
if (each <= pivot) {
left.push(each);
} else {
right.push(pivot);
}
});
return quickSort(left).concat(pivot, quickSort(right));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment