feat: Add Insertion sort

This commit is contained in:
2026-05-31 14:49:05 +05:30
parent f27f04ed1f
commit 9e613554b7
2 changed files with 26 additions and 1 deletions
+7 -1
View File
@@ -1,7 +1,13 @@
package main package main
import "fmt" import (
"fmt"
"dsa.go/pkg/algo"
)
func main() { func main() {
fmt.Println("Learning and Practicing DSA in Go.") fmt.Println("Learning and Practicing DSA in Go.")
algo.InsertionSort([]int{5, 2, 4, 6, 1, 3}, 6)
} }
+19
View File
@@ -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)
}