Skip to content

Instantly share code, notes, and snippets.

@jameslockwood
Created November 10, 2016 18:01
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 jameslockwood/ee5f40258ef3ff3b5c367584b6f9eb99 to your computer and use it in GitHub Desktop.
Save jameslockwood/ee5f40258ef3ff3b5c367584b6f9eb99 to your computer and use it in GitHub Desktop.
Quicksort implementation using JavaScript
function quickSort(arr = []) {
if (arr.length <= 1) {
return arr;
}
let left = [], right = [], same = [];
let mid = arr[Math.floor((arr.length - 1) / 2)];
for (let value of arr) {
if (value === mid) {
same.push(value);
} else if (value < mid) {
left.push(value);
} else {
right.push(value);
}
}
return [...quickSort(left), ...same, ...quickSort(right)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment