2 Star 2 Fork 9

王布衣/gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sort.go 828 Bytes
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-08-13 11:51 . 升级go版本到1.21.0
package partial
import (
"cmp"
"slices"
)
// Sort partially sorts a slice of any ordered type in ascending order.
// Only elements in x[:k] will be in sorted order. This is faster than using
// slices.Sort when k is small relative to the number of elements.
func Sort[E cmp.Ordered](x []E, k int) {
k = min(k, len(x))
if k > 0 {
floydRivest(x, 0, len(x)-1, k-1) // 0-indexed
slices.Sort(x[:k-1])
}
}
// SortFunc partially sorts the slice x in ascending order as determined by the
// less function. Only elements in x[:k] will be in sorted order. This is faster
// than using slices.SortFunc when k is small relative to the number of elements.
func SortFunc[E any](x []E, k int, less func(E, E) int) {
k = min(k, len(x))
if k > 0 {
floydRivestFunc(x, 0, len(x)-1, k-1, less)
slices.SortFunc(x[:k-1], less)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.15.5

搜索帮助