We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
0 parents commit e8be85dCopy full SHA for e8be85d
soring/insertion.go
@@ -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
38
0 commit comments