Created
June 24, 2020 04:34
-
-
Save educartoons/0b8021a665c21363bf0e12a73b31620f to your computer and use it in GitHub Desktop.
Implementation of Quick Sort in Go
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
// We will call quicksort with | |
// quickSort(array, 0, len(array)-1) | |
func quickSort(A []int, p int, r int) []int { | |
if p < r { | |
var q = partition(A, p, r) | |
quickSort(A, p, q-1) | |
quickSort(A, q+1, r) | |
} | |
return A | |
} | |
func partition(A []int, p int, r int) int { | |
var x = A[r] | |
var i = p - 1 | |
for j := p; j < r; j++ { | |
if A[j] <= x { | |
i = i + 1 | |
A[i], A[j] = A[j], A[i] | |
} | |
} | |
A[i+1], A[r] = A[r], A[i+1] | |
return i + 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment