Last active
February 22, 2023 00:51
-
-
Save xmapst/1ad3cda0f98a055349e3a2d729fd3d54 to your computer and use it in GitHub Desktop.
选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。
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 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