algorithms/sorting/insertion.go
2021-12-16 17:46:10 +02:00

12 lines
261 B
Go

package sorting
type insertion struct{}
func Insertion[T any](items []T, less func(a, b T) bool) {
len := len(items)
for i := 1; i < len; i++ {
for j := i; j > 0 && less(items[j], items[j-1]); j-- {
items[j], items[j-1] = items[j-1], items[j]
}
}
}