summaryrefslogtreecommitdiff
path: root/sort/insertion.go
blob: d14c275c12ff9864d72088e8c6ccef9233b0db12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package sort

import (
	"algorithms/ds"
)

func Insertion(a []ds.Comparer) []ds.Comparer {
	length := len(a)

	for i := 0; i < length; i++ {
		for j := i; j > 0; j-- {
			if a[j].HigherThan(a[j-1]) {
				break
			}
			tmp := a[j]
			a[j] = a[j-1]
			a[j-1] = tmp
		}
	}

	return a
}