Created
July 21, 2017 12:19
-
-
Save matthieu-D/56412d884e9b1ac10896307a6fb2e1ca to your computer and use it in GitHub Desktop.
This file contains 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
self.swap = function(arr, i, j) { | |
var temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
self.partition = function (arr, pivot, left, right){ | |
var pivotValue = arr[pivot], | |
partitionIndex = left; | |
for(var i = left; i < right; i++){ | |
if(arr[i] < pivotValue){ | |
swap(arr, i, partitionIndex); | |
partitionIndex++; | |
} | |
} | |
swap(arr, right, partitionIndex); | |
return partitionIndex; | |
} | |
self.quickSort = (arr, left, right) => { | |
var 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment