Skip to content

Instantly share code, notes, and snippets.

@nick-ivanov

nick-ivanov/quicksort.go

Last active Dec 31, 2017
Embed
What would you like to do?
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