forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion_sort_test.go
59 lines (49 loc) · 1.15 KB
/
insertion_sort_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Problem:
- Implement insertion sort.
Approach:
- Insert elements from an unsorted array into a sorted subsection of the
array, one item at a time.
Cost:
- O(n^2) time and O(1) space.
*/
package lab
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestInsertionSort(t *testing.T) {
tests := []struct {
in []int
expected []int
}{
{[]int{}, []int{}},
{[]int{1}, []int{1}},
{[]int{1, 2}, []int{1, 2}},
{[]int{2, 1}, []int{1, 2}},
{[]int{2, 1, 3}, []int{1, 2, 3}},
{[]int{1, 1, 1}, []int{1, 1, 1}},
{[]int{2, 1, 2}, []int{1, 2, 2}},
{[]int{1, 2, 4, 3, 6, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 2, 4, 3, 1, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 5, 4, 3, 2, 1}, []int{1, 2, 3, 4, 5, 6}},
}
for _, tt := range tests {
insertionSort(tt.in)
common.Equal(t, tt.expected, tt.in)
}
}
func insertionSort(in []int) {
// iterate through the list from position 1.
for i := 1; i < len(in); i++ {
// shift each one to the left by swapping it with the one before until
// it's in the right spot.
current := in[i]
j := i - 1
for j >= 0 && current < in[j] {
in[j+1] = in[j]
j--
}
in[j+1] = current
}
}