algorithms/sorting/selection.go

15 lines
274 B
Go
Raw Permalink Normal View History

2021-12-06 08:01:25 +02:00
package sorting
2021-12-16 17:46:10 +02:00
func Selection[T any](items []T, less func(a, b T) bool) {
len := len(items)
2021-12-06 08:01:25 +02:00
for i := 0; i < len; i++ {
min := i
for j := i + 1; j < len; j++ {
2021-12-16 17:46:10 +02:00
if less(items[j], items[min]) {
2021-12-06 08:01:25 +02:00
min = j
}
}
2021-12-16 17:46:10 +02:00
items[min], items[i] = items[i], items[min]
2021-12-06 08:01:25 +02:00
}
}