Skip to content

Instantly share code, notes, and snippets.

@imwally
Created April 26, 2016 15:37
Show Gist options
  • Save imwally/58d6bb9bf9da098064054f73a19cdca1 to your computer and use it in GitHub Desktop.
Save imwally/58d6bb9bf9da098064054f73a19cdca1 to your computer and use it in GitHub Desktop.
The quick sort algorithm in Go.
package main
import (
"fmt"
)
func partition(a []int, lo, hi int) int {
p := a[hi]
for j := lo; j < hi; j++ {
if a[j] < p {
a[j], a[lo] = a[lo], a[j]
lo++
}
}
a[lo], a[hi] = a[hi], a[lo]
return lo
}
func quickSort(a []int, lo, hi int) {
if lo > hi {
return
}
p := partition(a, lo, hi)
quickSort(a, lo, p-1)
quickSort(a, p+1, hi)
}
func main() {
list := []int{55, 90, 74, 20, 16, 46, 43, 59, 2, 99, 79, 10, 73, 1, 68, 56, 3, 87, 40, 78, 14, 18, 51, 24, 57, 89, 4, 62, 53, 23, 93, 41, 95, 84, 88}
quickSort(list, 0, len(list)-1)
fmt.Println(list)
}
@imwally
Copy link
Author

imwally commented Apr 26, 2016

Run on Go playground: https://play.golang.org/p/Ra5-i6kh2i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment