Skip to content

Instantly share code, notes, and snippets.

@xmapst
Last active February 22, 2023 00:52
Embed
What would you like to do?
希尔排序(Shell’s Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本。希尔排序是非稳定排序算法。该方法因D.L.Shell于1959年提出而得名。
package shellsort
func Sort[T constraints.Ordered](buf []T) {
length := len(buf)
incre := length
for {
incre /= 2
for k := 0; k < incre; k++ {
for i := k + incre; i < length; i += incre {
for j := i; j > k; j -= incre {
if buf[j] < buf[j-incre] {
tmp := buf[j-incre]
buf[j-incre] = buf[j]
buf[j] = tmp
} else {
break
}
}
}
}
if incre == 1 {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment