Skip to content

Instantly share code, notes, and snippets.

@ympons
Created March 2, 2017 01:20
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/3ab292a3f6e06f49f79f1abbce28cd83 to your computer and use it in GitHub Desktop.
Save ympons/3ab292a3f6e06f49f79f1abbce28cd83 to your computer and use it in GitHub Desktop.
Simple QuickSort implementation in Golang #1
package main
import (
"fmt"
"sort"
)
func quicksort(a sort.Interface, i, j int) {
if j > i {
left, right := i, j
piv := j
for left <= right {
for ;a.Less(left, piv); left++ {}
for ;a.Less(piv, right); right-- {}
if left <= right {
a.Swap(left, right)
left++
right--
}
}
quicksort(a, i, right)
quicksort(a, left, 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