go mysort 源码
golang mysort 代码
文件路径:/src/cmd/compile/internal/test/testdata/mysort/mysort.go
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Generic sort function, tested with two different pointer types.
package mysort
import (
"fmt"
)
type LessConstraint[T any] interface {
Less(T) bool
}
//go:noinline
func Sort[T LessConstraint[T]](x []T) {
n := len(x)
for i := 1; i < n; i++ {
for j := i; j > 0 && x[j].Less(x[j-1]); j-- {
x[j], x[j-1] = x[j-1], x[j]
}
}
}
type MyInt struct {
Value int
}
func (a *MyInt) Less(b *MyInt) bool {
return a.Value < b.Value
}
//go:noinline
func F() {
sl1 := []*MyInt{&MyInt{4}, &MyInt{3}, &MyInt{8}, &MyInt{7}}
Sort(sl1)
fmt.Printf("%v %v %v %v\n", sl1[0], sl1[1], sl1[2], sl1[3])
}
相关信息
相关文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦