Skip to content

Instantly share code, notes, and snippets.

@ejoubaud
Created November 12, 2019 17:11
Show Gist options
  • Save ejoubaud/88d4efeadddb792a5cd1e1a35da3091e to your computer and use it in GitHub Desktop.
Save ejoubaud/88d4efeadddb792a5cd1e1a35da3091e to your computer and use it in GitHub Desktop.
Sorts implemented in go
func QuickSort(a []int32) []int32 {
if len(a) < 2 {
return a
}
// lomuto partition
lastIndex := len(a) - 1
pivot := a[lastIndex]
j := -1
for i := range a[:lastIndex] {
if a[i] <= pivot {
j++
v1, v2 := a[i], a[j]
a[i], a[j] = v2, v1
}
}
j++
v1, v2 := a[lastIndex], a[j]
a[len(a)-1], a[j] = v2, v1
QuickSort(a[:j])
QuickSort(a[j:])
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment