commit 1d68733dd865635379c22354afb91c5070677046 Author: clerie Date: Sat Oct 21 14:06:50 2023 +0200 Init repo diff --git a/aud/insertionsort.go b/aud/insertionsort.go new file mode 100644 index 0000000..d2d37a8 --- /dev/null +++ b/aud/insertionsort.go @@ -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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a065384 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module clerie.de/letsgo + +go 1.20 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go new file mode 100644 index 0000000..0044da9 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "clerie.de/letsgo/aud" + +func main() { + aud.InsertionSort([]int {2,6,1,3,5,4}) +}