Skip to content

Instantly share code, notes, and snippets.

@revanth0212
Created April 21, 2018 19:41
Show Gist options
  • Save revanth0212/854035de50fa70b6bca8b56a6ca0c00e to your computer and use it in GitHub Desktop.
Save revanth0212/854035de50fa70b6bca8b56a6ca0c00e to your computer and use it in GitHub Desktop.
Quick Sort
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