MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / BinaryInsertion

Function BinaryInsertion

sort/binaryinsertionsort.go:13–35  ·  view source on GitHub ↗
(arr []T)

Source from the content-addressed store, hash-verified

11import "github.com/TheAlgorithms/Go/constraints"
12
13func BinaryInsertion[T constraints.Ordered](arr []T) []T {
14 for currentIndex := 1; currentIndex < len(arr); currentIndex++ {
15 temporary := arr[currentIndex]
16 low := 0
17 high := currentIndex - 1
18
19 for low <= high {
20 mid := low + (high-low)/2
21 if arr[mid] > temporary {
22 high = mid - 1
23 } else {
24 low = mid + 1
25 }
26 }
27
28 for itr := currentIndex; itr > low; itr-- {
29 arr[itr] = arr[itr-1]
30 }
31
32 arr[low] = temporary
33 }
34 return arr
35}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected