Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.