希尔排序(Shell’s Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本。希尔排序是非稳定排序算法。该方法因D.L.Shell于1959年提出而得名。
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 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