Skip to content

Instantly share code, notes, and snippets.

@ckeyer
Created July 13, 2015 14:32
Show Gist options
  • Save ckeyer/cbdbd7425aece664c9e2 to your computer and use it in GitHub Desktop.
Save ckeyer/cbdbd7425aece664c9e2 to your computer and use it in GitHub Desktop.
希尔排序 是插入排序的一种。也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本。
package main
import (
"fmt"
"sort"
)
func ShellSort(data interface{}, args ...int) {
if v, ok := data.(sort.Interface); ok {
for gap := v.Len() / 2; gap > 0; gap /= 2 {
for i := gap; i < v.Len(); i++ {
for j := i - gap; j >= 0 && v.Less(j+gap, j); j -= gap {
v.Swap(j, j+gap)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment