Created
March 2, 2017 01:27
-
-
Save ympons/477a01248e68e97db447400c58668d1f to your computer and use it in GitHub Desktop.
Another QuickSort implementation in Golang
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
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
func quickSort(a sort.Interface, left, right int) { | |
if left >= right { | |
return | |
} | |
piv := partition(a, left, right, right) | |
quickSort(a, left, piv-1) | |
quickSort(a, piv+1, right) | |
} | |
func partition(a sort.Interface, left int, right int, piv int) int { | |
j := left | |
for i := left; i <= right; i++ { | |
if a.Less(i, piv) { | |
a.Swap(i, j) | |
j++ | |
} | |
} | |
a.Swap(piv, j) | |
return j | |
} | |
func main() { | |
a := []int{1, 3, 9, 8, 2, 7, 5} | |
fmt.Println("unsorted: ", a) | |
quickSort(sort.IntSlice(a), 0, len(a)-1) | |
fmt.Println("Sorted: ", a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment