From 6e5dda890ea64f25eafd3c074bf584d0833bbbce Mon Sep 17 00:00:00 2001 From: randheerrrk <> Date: Thu, 5 Oct 2023 22:53:18 +0530 Subject: [PATCH 1/2] feat: add binary insertion sort algorithm --- sort/binaryinsertionsort.go | 34 ++++++++++++++++++++++++++++++++++ sort/sorts_test.go | 7 +++++++ 2 files changed, 41 insertions(+) create mode 100644 sort/binaryinsertionsort.go diff --git a/sort/binaryinsertionsort.go b/sort/binaryinsertionsort.go new file mode 100644 index 000000000..b3e983888 --- /dev/null +++ b/sort/binaryinsertionsort.go @@ -0,0 +1,34 @@ +// Binary Insertion Sort +// description: Implementation of binary insertion sort in Go +// details: Binary Insertion Sort is a variation of +// Insertion sort in which proper location to +// insert the selected element is found using the +// Binary search algorithm. + +package sort + +import "github.com/TheAlgorithms/Go/constraints" + +func BinaryInsertion[T constraints.Ordered](arr []T) []T { + for currentIndex := 1; currentIndex < len(arr); currentIndex++ { + temporary := arr[currentIndex] + low := 0 + high := currentIndex - 1 + + for low <= high { + mid := low + (high - low) / 2 + if arr[mid] > temporary { + high = mid - 1 + } else { + low = mid + 1 + } + } + + for itr := currentIndex; itr > low; itr-- { + arr[itr] = arr[itr-1] + } + + arr[low] = temporary + } + return arr +} diff --git a/sort/sorts_test.go b/sort/sorts_test.go index aeb90c04e..cc50fbe5e 100644 --- a/sort/sorts_test.go +++ b/sort/sorts_test.go @@ -76,6 +76,9 @@ func testFramework(t *testing.T, sortingFunction func([]int) []int) { } //BEGIN TESTS +func TestBinaryInsertion(t *testing.T) { + testFramework(t, sort.BinaryInsertion[int]) +} func TestBubble(t *testing.T) { testFramework(t, sort.Bubble[int]) @@ -214,6 +217,10 @@ func benchmarkFramework(b *testing.B, f func(arr []int) []int) { //BEGIN BENCHMARKS +func BenchmarkBinaryInsertion(b *testing.B) { + benchmarkFramework(b, sort.BinaryInsertion[int]) +} + func BenchmarkBubble(b *testing.B) { benchmarkFramework(b, sort.Bubble[int]) } From 4d9031e1f11ba53b0db8f43d8b849d88da72c2e4 Mon Sep 17 00:00:00 2001 From: randheerrrk <> Date: Fri, 6 Oct 2023 21:56:32 +0530 Subject: [PATCH 2/2] Formatted with gofmt --- sort/binaryinsertionsort.go | 3 ++- sort/sorts_test.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sort/binaryinsertionsort.go b/sort/binaryinsertionsort.go index b3e983888..98b6c5a3d 100644 --- a/sort/binaryinsertionsort.go +++ b/sort/binaryinsertionsort.go @@ -4,6 +4,7 @@ // Insertion sort in which proper location to // insert the selected element is found using the // Binary search algorithm. +// ref: https://www.geeksforgeeks.org/binary-insertion-sort package sort @@ -16,7 +17,7 @@ func BinaryInsertion[T constraints.Ordered](arr []T) []T { high := currentIndex - 1 for low <= high { - mid := low + (high - low) / 2 + mid := low + (high-low)/2 if arr[mid] > temporary { high = mid - 1 } else { diff --git a/sort/sorts_test.go b/sort/sorts_test.go index cc50fbe5e..d895e2c75 100644 --- a/sort/sorts_test.go +++ b/sort/sorts_test.go @@ -75,7 +75,7 @@ func testFramework(t *testing.T, sortingFunction func([]int) []int) { } } -//BEGIN TESTS +// BEGIN TESTS func TestBinaryInsertion(t *testing.T) { testFramework(t, sort.BinaryInsertion[int]) }