Skip to content

Instantly share code, notes, and snippets.

@thebopshoobop
Created October 2, 2017 05:54
Show Gist options
  • Save thebopshoobop/1832f6c9af5db4515b6484bcd5878b91 to your computer and use it in GitHub Desktop.
Save thebopshoobop/1832f6c9af5db4515b6484bcd5878b91 to your computer and use it in GitHub Desktop.
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