algorithms/sorting/insertion.go

13 lines
261 B
Go
Raw Normal View History

2021-12-14 12:35:28 +02:00
package sorting
type insertion struct{}
2021-12-16 17:46:10 +02:00
func Insertion[T any](items []T, less func(a, b T) bool) {
len := len(items)
2021-12-14 12:35:28 +02:00
for i := 1; i < len; i++ {
2021-12-16 17:46:10 +02:00
for j := i; j > 0 && less(items[j], items[j-1]); j-- {
items[j], items[j-1] = items[j-1], items[j]
2021-12-14 12:35:28 +02:00
}
}
}