algorithms/sorting/selection.go
2021-12-06 08:01:25 +02:00

20 lines
302 B
Go

package sorting
type selection struct{}
func (*selection) Sort(items Sortable) {
len := items.Len()
for i := 0; i < len; i++ {
min := i
for j := i + 1; j < len; j++ {
if items.Less(j, min) {
min = j
}
}
items.Swap(min, i)
}
}
func NewSelection() Sorter {
return &selection{}
}