1
This commit is contained in:
parent
01573c9947
commit
3eeb9788ce
29
algorithm.go
Normal file
29
algorithm.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package q5
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
type sortImpl[T any] struct {
|
||||||
|
data []*T
|
||||||
|
lessCb func(*T, *T) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *sortImpl[T]) Len() int {
|
||||||
|
return len(this.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *sortImpl[T]) Swap(i int, j int) {
|
||||||
|
this.data[i], this.data[j] = this.data[j], this.data[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *sortImpl[T]) Less(i int, j int) bool {
|
||||||
|
return this.lessCb(this.data[i], this.data[j])
|
||||||
|
}
|
||||||
|
|
||||||
|
func Sort[T any](data []*T, lessCb func(*T, *T) bool) {
|
||||||
|
impl := new(sortImpl[T])
|
||||||
|
impl.data = data
|
||||||
|
impl.lessCb = lessCb
|
||||||
|
sort.Sort(impl)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user