Skip to content

Instantly share code, notes, and snippets.

@Eder87rh
Created July 30, 2019 23:26
Show Gist options
  • Select an option

  • Save Eder87rh/989c68b2c285c180cae4fe0db5bcb5e4 to your computer and use it in GitHub Desktop.

Select an option

Save Eder87rh/989c68b2c285c180cae4fe0db5bcb5e4 to your computer and use it in GitHub Desktop.
pivot-helper.js
function pivot(arr, start=0, end=arr.length + 1) {
function swap(array, i, j) {
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
var pivot = arr[start];
var swapIdx = start;
for (var i = start + 1; i < arr.length; i++) {
if (pivot > arr[i]) {
swapIdx++;
swap(arr, swapIdx, i);
}
}
swap(arr, start, swapIdx);
console.log(arr)
return swapIdx;
}
pivot([4, 8, 2, 1, 5, 7, 6, 3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment