-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinaryinsertionsort.py
More file actions
32 lines (27 loc) · 945 Bytes
/
Copy pathbinaryinsertionsort.py
File metadata and controls
32 lines (27 loc) · 945 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
from utils import read_txt as rtxt
import time
selected_file, list_of_numbers = rtxt.read_file_content()
print(list_of_numbers)
print("#######################################################")
start_time = time.time()
def binary_search(array, start, end, to_find):
while start <= end:
middle = (start + end) // 2
if array[middle] > to_find:
end = middle - 1
elif array[middle] < to_find:
start = middle + 1
else:
return middle
return start
def binary_insertion_sort(array):
for i in range(1, len(array)):
to_insert = array[i]
to_insert_index = binary_search(array, 0, i - 1, to_insert)
copy = i
while copy > to_insert_index:
array[copy] = array[copy - 1]
copy -= 1
array[copy] = to_insert
binary_insertion_sort(list_of_numbers)
print(f'sorted: {list_of_numbers}')