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

import (
	"algorithms/ds"
)

func Insertion(a ds.ArrayList) ds.ArrayList {
	length := len(a)

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

	return a
}