From 95322d872b1dab67b33ce82fa56d1e7a42ffa8c6 Mon Sep 17 00:00:00 2001 From: gopheramit Date: Sat, 2 Oct 2021 11:25:06 +0530 Subject: [PATCH] implemented insertion sort --- Algorithms/insertion_sort.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Algorithms/insertion_sort.go diff --git a/Algorithms/insertion_sort.go b/Algorithms/insertion_sort.go new file mode 100644 index 0000000..54ebbba --- /dev/null +++ b/Algorithms/insertion_sort.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func insertioSort(arr []int) []int { + for i := 1; i < len(arr); i++ { + j := i + for j > 0 { + if arr[j-1] > arr[j] { + arr[j-1], arr[j] = arr[j], arr[j-1] + } + j = j - 1 + } + } + return arr +} +func main() { + items := []int{45, 100, 1, 20, 31, 63, 70, 2, 9} + fmt.Println(insertioSort(items)) +}