Created
July 11, 2018 11:40
-
-
Save e-mihaylin/883d6d1309e9d99faae61b88789deffd to your computer and use it in GitHub Desktop.
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
| const quickSort = (arr, left, right) => { | |
| let len = arr.length, | |
| pivot, | |
| partitionIndex; | |
| if (left < right){ | |
| pivot = right; | |
| partitionIndex = partition(arr, pivot, left, right); | |
| //sort left and right | |
| quickSort(arr, left, partitionIndex - 1); | |
| quickSort(arr, partitionIndex + 1, right); | |
| } | |
| return arr; | |
| } | |
| const partition = (arr, pivot, left, right) => { | |
| let pivotValue = arr[pivot], | |
| partitionIndex = left; | |
| for (let i = left; i < right; i++){ | |
| if (arr[i] < pivotValue){ | |
| swap(arr, i, partitionIndex); | |
| partitionIndex++; | |
| } | |
| } | |
| swap(arr, right, partitionIndex); | |
| return partitionIndex; | |
| } | |
| const swap = (arr, i, j) => { | |
| let temp = arr[i]; | |
| arr[i] = arr[j]; | |
| arr[j] = temp; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment