Skip to content

Instantly share code, notes, and snippets.

@xmapst
Last active February 22, 2023 00:51
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/1ad3cda0f98a055349e3a2d729fd3d54 to your computer and use it in GitHub Desktop.
Save xmapst/1ad3cda0f98a055349e3a2d729fd3d54 to your computer and use it in GitHub Desktop.
选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。
package query
func Sort[T constraints.Ordered](buf []T) {
for i := 0; i < len(buf)-1; i++ {
min := i
for j := i; j < len(buf); j++ {
if buf[min] > buf[j] {
min = j
}
}
if min != i {
tmp := buf[i]
buf[i] = buf[min]
buf[min] = tmp
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment