MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / binary_search_insertion

Function binary_search_insertion

sorts/merge_insertion_sort.py:17–35  ·  view source on GitHub ↗

>>> binary_search_insertion([1, 2, 7, 9, 10], 4) [1, 2, 4, 7, 9, 10]

(sorted_list, item)

Source from the content-addressed store, hash-verified

15
16
17def binary_search_insertion(sorted_list, item):
18 """
19 >>> binary_search_insertion([1, 2, 7, 9, 10], 4)
20 [1, 2, 4, 7, 9, 10]
21 """
22 left = 0
23 right = len(sorted_list) - 1
24 while left <= right:
25 middle = (left + right) // 2
26 if left == right:
27 if sorted_list[middle] < item:
28 left = middle + 1
29 break
30 elif sorted_list[middle] < item:
31 left = middle + 1
32 else:
33 right = middle - 1
34 sorted_list.insert(left, item)
35 return sorted_list
36
37
38def merge(left, right):

Callers 1

merge_insertion_sortFunction · 0.85

Calls 1

insertMethod · 0.45

Tested by

no test coverage detected