Skip to content

Instantly share code, notes, and snippets.

@xmapst
Last active February 22, 2023 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmapst/fb7b0804d9c1a0f27599a7b0664c0d63 to your computer and use it in GitHub Desktop.
Save xmapst/fb7b0804d9c1a0f27599a7b0664c0d63 to your computer and use it in GitHub Desktop.
一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序,这个时候就要用到一种新的排序方法——插入排序法
func Sort[T constraints.Ordered](buf []T) {
for i := 1; i < len(buf); i++ {
for j := i; j > 0; j-- {
if buf[j] < buf[j-1] {
tmp := buf[j-1]
buf[j-1] = buf[j]
buf[j] = tmp
} else {
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment