Skip to content

Instantly share code, notes, and snippets.

@nick-ivanov
Last active December 31, 2017 02:40
Show Gist options
  • Save nick-ivanov/c28ee60e9b23d04decc399fafbb1f5c0 to your computer and use it in GitHub Desktop.
Save nick-ivanov/c28ee60e9b23d04decc399fafbb1f5c0 to your computer and use it in GitHub Desktop.
QuickSort using Golang
package main
type qsintarray []int
func (q qsintarray) nnQuickSortInt() {
q.nnQuickSortIntWorker(0, len(q) - 1)
}
func (q qsintarray) nnQuickSortIntWorker(start int, end int) {
if start >= end {
return
}
midIndex := (end+start) / 2
midValue := q[midIndex]
west, east := start, end
for west <= east {
for q[west] < midValue {
west++
}
for q[east] > midValue {
east--
}
if west <= east {
q[west], q[east] = q[east], q[west]
west++
east--
}
}
if west < end {
q.nnQuickSortIntWorker(west, end)
}
if east > start {
q.nnQuickSortIntWorker(start, east)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment