Skip to content

Instantly share code, notes, and snippets.

@Shurlow
Created April 10, 2017 16:36
Show Gist options
  • Save Shurlow/8d815dc63b30b0eca27d1fd29b501574 to your computer and use it in GitHub Desktop.
Save Shurlow/8d815dc63b30b0eca27d1fd29b501574 to your computer and use it in GitHub Desktop.
function swap(arr, idx1, idx2) {
let temp = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = temp;
}
function partition(arr, left, right) {
let pivotValue = arr[left];
let partitionIndex = left;
for (let i = left + 1; i <= right; i++) {
if (arr[i] < pivotValue) {
partitionIndex++;
swap(arr, i, partitionIndex);
}
}
swap(arr, left, partitionIndex);
return partitionIndex;
}
function unknownAlgorithm(arr, left=0, right=arr.length - 1) {
if (left < right) {
let partitionIndex = partition(arr, left, right);
unknownAlgorithm(arr, left, partitionIndex - 1);
unknownAlgorithm(arr, partitionIndex + 1, right);
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment