Init repo

This commit is contained in:
clerie 2023-10-21 14:06:50 +02:00
commit 1d68733dd8
4 changed files with 48 additions and 0 deletions

38
aud/insertionsort.go Normal file
View File

@ -0,0 +1,38 @@
package aud
import "fmt"
func InsertionSort(list []int) []int {
fmt.Println(list)
for round := 1; round < len(list); round++ {
// Element to sort is list[round]
// Elements that are already sorted are list[0:round-1]
// Elements that are unsorted are list[round+1:len(list)]
element_to_insert := list[round]
// Find the first position where an element in bigger than the
// one we want to insert
position_to_insert := round
for i := 0; i < round; i++ {
if list[i] > element_to_insert {
position_to_insert = i
break
}
}
fmt.Sprintf("Insert element at %v", position_to_insert)
for i := round; i > position_to_insert; i-- {
list[i] = list[i-1]
}
list[position_to_insert] = element_to_insert
fmt.Println(list)
}
return list
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module clerie.de/letsgo
go 1.20

0
go.sum Normal file
View File

7
main.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "clerie.de/letsgo/aud"
func main() {
aud.InsertionSort([]int {2,6,1,3,5,4})
}