-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathinsertion-sort.go
55 lines (48 loc) · 1.26 KB
/
insertion-sort.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
/*
Date: 2023-12-29
Description:
Implement insertion sort.
Approach:
Scans from right in sorted sub-array and inserts current element at correct
place by shifting all the elements to right.
Complexity:
O(n^2)
*/
package main
import (
"fmt"
"reflect"
)
// Sorts slice in increasing order using insertion sort.
func insertionSort(nums []int) {
var j int
for i := 1; i < len(nums); i++ {
key := nums[i]
// Loop until elements are sorted.
// For decreasing order, reverse the condition.
for j = i; j > 0 && nums[j-1] > key; j-- {
nums[j] = nums[j-1]
}
nums[j] = key
}
}
func main() {
testCases := []struct {
input, expected []int
}{
{input: []int{5, 4, 3, 2, 1, 10, 28, 7, 6}, expected: []int{1, 2, 3, 4, 5, 6, 7, 10, 28}},
{input: []int{3, 4, 5, 2, 1}, expected: []int{1, 2, 3, 4, 5}},
{input: []int{3, 4, 5, 2, 1, 6}, expected: []int{1, 2, 3, 4, 5, 6}},
{input: []int{}, expected: []int{}},
{input: []int{1}, expected: []int{1}},
{input: []int{2, 1}, expected: []int{1, 2}},
}
for idx, tc := range testCases {
inputCopy := make([]int, len(tc.input))
copy(inputCopy, tc.input)
insertionSort(inputCopy)
if !reflect.DeepEqual(inputCopy, tc.expected) {
fmt.Printf("\nTC %d failed. Expected %v, got %v", idx, tc.expected, inputCopy)
}
}
}