Skip to content

Instantly share code, notes, and snippets.

@mykoweb
Created February 13, 2017 00:11
Show Gist options
  • Save mykoweb/f7fb62a60e8c9e43b97aac85ed6dde0e to your computer and use it in GitHub Desktop.
Save mykoweb/f7fb62a60e8c9e43b97aac85ed6dde0e to your computer and use it in GitHub Desktop.
// This version of QuickSort reverts to InsertionSort
// when the slice size is below a cutoff value of 12.
// Add ShellSort before InsertionSort to help InsertionSort.
func QuickSort(data Interface, l int, r int, enableShellSort bool) {
if l >= r {
return
}
if r-l > 12 {
pivot := partition(data, l, r)
QuickSort(data, l, pivot-1, enableShellSort)
QuickSort(data, pivot+1, r, enableShellSort)
} else {
if enableShellSort {
// From Go source.
// Do ShellSort pass with gap 6
for i := l + 6; i < r; i++ {
if data.Less(i, i-6) {
data.Swap(i, i-6)
}
}
}
insertionSort(data, l, r)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment