-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.1-insertion-sort.go
58 lines (47 loc) · 984 Bytes
/
2.1-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
56
57
58
package main
import "fmt"
func insertionSort(A []int) []int {
for j := 1; j < len(A); j++ {
key := A[j]
// Insert A[j] into the sorted sequence A[1 .. j - 1].
i := j - 1
for i >= 0 && A[i] > key {
A[i+1] = A[i]
i--
}
A[i+1] = key
}
return A
}
func main() {
A := [7]int{5, 6, 1, 7, 4, 9, 0}
fmt.Println("Original:", A)
fmt.Println("Sorted:", insertionSort(A[:]))
fmt.Println("Sorted non-increasing:", insertionSortNonIncreasing(A[:]))
fmt.Println("Linear search 9:", linearSearch(A[:], 9))
fmt.Println("Linear search 11:", linearSearch(A[:], 11))
}
// 2.1-2
func insertionSortNonIncreasing(A []int) []int {
for j := 1; j < len(A); j++ {
key := A[j]
// Insert A[j] into the sorted sequence A[1 .. j - 1].
i := j - 1
for i >= 0 && A[i] < key {
A[i+1] = A[i]
i--
}
A[i+1] = key
}
return A
}
// 2.1-3
func linearSearch(A []int, v int) int {
for i := 0; i < len(A); i++ {
if A[i] == v {
return i
}
}
return -1
}
// 2.1-4