Created
July 30, 2019 23:26
-
-
Save Eder87rh/989c68b2c285c180cae4fe0db5bcb5e4 to your computer and use it in GitHub Desktop.
pivot-helper.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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