Created
April 21, 2018 19:41
-
-
Save revanth0212/854035de50fa70b6bca8b56a6ca0c00e to your computer and use it in GitHub Desktop.
Quick Sort
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 swap(arr, i, j) { | |
var temp = arr[i] | |
arr[i] = arr[j] | |
arr[j] = temp | |
return arr | |
} | |
function partition(arr, start, end) { | |
var pivot = end | |
while(start <= end) { | |
if (arr[start] < arr[pivot]) { | |
start++ | |
} else if (arr[end] > pivot) { | |
end-- | |
} else { | |
swap(arr, start, end) | |
start++ | |
end-- | |
} | |
} | |
swap(arr, start, pivot) | |
return start | |
} | |
function quickSort(arr, start, end) { | |
if (end > start) { | |
var newPivot = partition(arr, start, end) | |
quickSort(arr, start, newPivot-1) | |
quickSort(arr, newPivot+1, end) | |
} | |
} | |
function sort(arr) { | |
quickSort(arr, 0, arr.length-1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment