Skip to content

Commit e8be85d

Browse files
committed
first commit
0 parents  commit e8be85d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

soring/insertion.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
)
7+
8+
func insertionSort(slice []int) {
9+
n := len(slice)
10+
for i := 1; i < n; i++ {
11+
// fmt.Printf("Iteration %d:\n", i)
12+
for j := i; j > 0; j-- {
13+
if slice[j-1] > slice[j] {
14+
slice[j-1], slice[j] = slice[j], slice[j-1]
15+
} else {
16+
break
17+
}
18+
}
19+
// fmt.Println(slice)
20+
}
21+
}
22+
23+
func generateSlice(size int) []int {
24+
slice := make([]int, size)
25+
26+
for i := range slice {
27+
slice[i] = rand.Intn(100) + 1
28+
}
29+
30+
return slice
31+
}
32+
33+
func main() {
34+
sl := generateSlice(10)
35+
fmt.Println(sl)
36+
insertionSort(sl)
37+
fmt.Println(sl)
38+
}

0 commit comments

Comments
 (0)