-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathbinaryinsertionsort.go
More file actions
35 lines (29 loc) · 838 Bytes
/
binaryinsertionsort.go
File metadata and controls
35 lines (29 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 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.
// ref: https://www.geeksforgeeks.org/binary-insertion-sort
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
}