diff --git a/cmd/main.go b/cmd/main.go index c6d8c70..80ca6b8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,7 +1,13 @@ package main -import "fmt" +import ( + "fmt" + + "dsa.go/pkg/algo" +) func main() { fmt.Println("Learning and Practicing DSA in Go.") + + algo.InsertionSort([]int{5, 2, 4, 6, 1, 3}, 6) } diff --git a/pkg/algo/sorting.go b/pkg/algo/sorting.go new file mode 100644 index 0000000..9220e27 --- /dev/null +++ b/pkg/algo/sorting.go @@ -0,0 +1,19 @@ +package algo + +import "fmt" + +func InsertionSort(a []int, n int) { + fmt.Println("-- Insertion Sort --") + fmt.Printf("Input: %v\n", a) + + for i := 1; i < n; i++ { + key := a[i] + j := i - 1 + for ; j >= 0 && a[j] > key; j-- { + a[j+1] = a[j] + } + a[j+1] = key + } + + fmt.Printf("Output: %v\n", a) +}