Last active
December 31, 2017 02:40
-
-
Save nick-ivanov/c28ee60e9b23d04decc399fafbb1f5c0 to your computer and use it in GitHub Desktop.
QuickSort using Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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