-
Notifications
You must be signed in to change notification settings - Fork 55
/
BinaryInsertionSort.py
36 lines (32 loc) · 1.1 KB
/
BinaryInsertionSort.py
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
36
class Sort:
def __init__(self, array):
self.array = array
def binary_insertion_sort(self):
for i in range(1, len(self.array)):
key = self.array[i]
pos = self.binary_search(key, 0, i) + 1
for k in range(i, pos, -1):
self.array[k] = self.array[k - 1]
self.array[pos] = key
return self.array
def binary_search(self, element, first, last):
if last - first <= 1:
if element < self.array[first]:
return first - 1
else:
return first
mid = (first + last)//2
if self.array[mid] < element:
return self.binary_search(element, mid, last)
elif self.array[mid] > element:
return self.binary_search(element, first, mid)
else:
return mid
def main():
array = list(map(int, input("Enter unsorted array: ").split(' ')))
obj = Sort(array)
sorted_array = obj.binary_insertion_sort()
print("Sorted Array: ", end = '')
print(*sorted_array)
if __name__ == "__main__":
main()