Initial commit with insertion sort
This commit is contained in:
commit
f75bc1902b
3 changed files with 84 additions and 0 deletions
13
cmd/main.go
Normal file
13
cmd/main.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
insertionsort "git.pro100code.com/corman/internal/insertion_sort"
|
||||
)
|
||||
|
||||
func main() {
|
||||
input := insertionsort.TEST_INPUT
|
||||
result := insertionsort.InsertionSortBasic(input)
|
||||
fmt.Print(result)
|
||||
}
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module git.pro100code.com/corman
|
||||
|
||||
go 1.23.3
|
68
internal/insertion_sort/insertion_sort.go
Normal file
68
internal/insertion_sort/insertion_sort.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package insertionsort
|
||||
|
||||
var TEST_INPUT []int = []int{
|
||||
45,
|
||||
9,
|
||||
79,
|
||||
36,
|
||||
20,
|
||||
95,
|
||||
35,
|
||||
72,
|
||||
55,
|
||||
2,
|
||||
16,
|
||||
77,
|
||||
58,
|
||||
46,
|
||||
30,
|
||||
49,
|
||||
92,
|
||||
61,
|
||||
52,
|
||||
94,
|
||||
26,
|
||||
76,
|
||||
14,
|
||||
1,
|
||||
57,
|
||||
33,
|
||||
38,
|
||||
66,
|
||||
11,
|
||||
85,
|
||||
83,
|
||||
65,
|
||||
3,
|
||||
15,
|
||||
74,
|
||||
41,
|
||||
90,
|
||||
89,
|
||||
88,
|
||||
96,
|
||||
64,
|
||||
71,
|
||||
10,
|
||||
60,
|
||||
75,
|
||||
91,
|
||||
93,
|
||||
84,
|
||||
6,
|
||||
22,
|
||||
}
|
||||
|
||||
|
||||
func InsertionSortBasic(input []int) []int {
|
||||
for j := 1; j < len(input); j++ {
|
||||
key := input[j]
|
||||
i := j - 1
|
||||
for (i >= 0) && (input[i] > key) {
|
||||
input[i+1] = input[i]
|
||||
i = i - 1
|
||||
}
|
||||
input[i+1] = key
|
||||
}
|
||||
return input
|
||||
}
|
Loading…
Reference in a new issue