Skip to content

Instantly share code, notes, and snippets.

@ympons
Created March 2, 2017 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ympons/477a01248e68e97db447400c58668d1f to your computer and use it in GitHub Desktop.
Save ympons/477a01248e68e97db447400c58668d1f to your computer and use it in GitHub Desktop.
Another QuickSort implementation in Golang
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