-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_insertion_sort.py
65 lines (56 loc) · 1.47 KB
/
binary_insertion_sort.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from time import time
from random import randint
def insertion_sort(arr, n):
for j in range(1, n):
key = arr[j]
i = j - 1
low = 0
high = j
while low < high:
mid = (low + high) // 2
if key >= arr[mid]:
low = mid + 1
else:
high = mid
while i >= low:
arr[i + 1]= arr[i]
i -= 1
arr[i + 1] = key
def main():
arr = [randint(-10,10) for i in range(8)]
print (arr)
insertion_sort(arr, len(arr))
print (arr)
if __name__ == "__main__":
main()
"""
def binarysearch(arr, key, low, high):
if high <= low:
if key > arr[low]:
return low + 1
return low
mid = (low + high) / 2
if key == arr[mid]:
return mid + 1
if key < arr[mid]:
return binarysearch(arr, key, low, mid - 1)
return binarysearch(arr, key, mid + 1, high)
def insertionsort(arr, n):
for j in range(1, n):
i = j - 1
key = arr[j]
pos = binarysearch(arr, key, 0, i)
while i >= pos:
arr[i + 1] = arr[i]
i -= 1
arr[i + 1] = key
def main():
print "Enter a bunch of numbers..."
arr = [randint(1,100000) for i in xrange(10000)]
start = time()
insertionsort(arr, len(arr))
print arr
print time() - start
if __name__ == "__main__":
main()
"""