Skip to content

Instantly share code, notes, and snippets.

@felipernb
Created August 30, 2012 14:20
Show Gist options
  • Select an option

  • Save felipernb/3529466 to your computer and use it in GitHub Desktop.

Select an option

Save felipernb/3529466 to your computer and use it in GitHub Desktop.
package quicksort
func Quicksort(a []int) []int {
if (len(a) <= 1) {
return a
}
p := partition(a)
Quicksort(a[0:p])
Quicksort(a[p+1:])
return a
}
func partition(a []int) int {
pivot := a[0]
i := 1
for j := i; j < len(a); j++ {
if a[j] < pivot {
a[j], a[i] = a[i], a[j]
i++
}
}
a[0], a[i-1] = a[i-1], a[0]
return i-1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment