Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick Sort implemented in JS
function quickSort(array, left = 0, right = array.length - 1) {
console.log("CALLED");
let partitionIndex;
let pivotIndex;
if(left < right) {
pivotIndex = right;
partitionIndex = partition(array, left, right, pivotIndex);
quickSort(array, left, partitionIndex - 1);
quickSort(array, partitionIndex + 1, right);
}
return array;
}
function partition(a, left, right, pivotIndex) {
let partitionIndex = left,
pivot = array[pivotIndex];
for(let i = left; i < right; i++) {
if(a[i] < pivot) {
[a[i], a[partitionIndex]] = [a[partitionIndex], a[i]];
partitionIndex += 1
}
}
[a[right], a[partitionIndex]] = [a[partitionIndex], a[right]];
return partitionIndex;
}
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.