Skip to content

Commit a97ab2f

Browse files
Update timsort.py
1 parent 1958cf2 commit a97ab2f

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

sorts/timsort.py

+49-36
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,81 @@
1-
def binary_search(the_array, item, start, end):
1+
def binary_search(lst, item, start, end):
22
if start == end:
3-
if the_array[start] > item:
3+
if lst[start] > item:
44
return start
55
else:
66
return start + 1
77
if start > end:
88
return start
99

10-
mid = (start + end)/ 2
11-
if the_array[mid] < item:
12-
return binary_search(the_array, item, mid + 1, end)
13-
elif the_array[mid] > item:
14-
return binary_search(the_array, item, start, mid - 1)
10+
mid = (start + end) // 2
11+
if lst[mid] < item:
12+
return binary_search(lst, item, mid + 1, end)
13+
elif lst[mid] > item:
14+
return binary_search(lst, item, start, mid - 1)
1515
else:
1616
return mid
1717

1818

19-
"""
20-
Insertion sort that the heap sort uses if the array size is small or if
21-
the size of the "run" is small
22-
"""
23-
def insertion_sort(the_array):
24-
l = len(the_array)
25-
for index in range(1, l):
26-
value = the_array[index]
27-
pos = binary_search(the_array, value, 0, index - 1)
28-
the_array = the_array[:pos] + [value] + the_array[pos:index] + the_array[index+1:]
29-
return the_array
19+
def insertion_sort(lst):
20+
length = len(lst)
21+
22+
for index in range(1, length):
23+
value = lst[index]
24+
pos = binary_search(lst, value, 0, index - 1)
25+
lst = lst[:pos] + [value] + lst[pos:index] + lst[index+1:]
26+
27+
return lst
28+
3029

3130
def merge(left, right):
32-
"""Takes two sorted lists and returns a single sorted list by comparing the
33-
elements one at a time.
34-
[1, 2, 3, 4, 5, 6]
35-
"""
3631
if not left:
3732
return right
33+
3834
if not right:
3935
return left
36+
4037
if left[0] < right[0]:
4138
return [left[0]] + merge(left[1:], right)
39+
4240
return [right[0]] + merge(left, right[1:])
4341

4442

45-
def timsort(the_array):
43+
def timsort(lst):
4644
runs, sorted_runs = [], []
47-
l = len(the_array)
48-
new_run = [the_array[0]]
49-
for i in range(1, l):
50-
if i == l-1:
51-
new_run.append(the_array[i])
45+
length = len(lst)
46+
new_run = [lst[0]]
47+
sorted_array = []
48+
49+
for i in range(1, length):
50+
if i == length - 1:
51+
new_run.append(lst[i])
5252
runs.append(new_run)
5353
break
54-
if the_array[i] < the_array[i-1]:
54+
55+
if lst[i] < lst[i - 1]:
5556
if not new_run:
56-
runs.append([the_array[i-1]])
57-
new_run.append(the_array[i])
57+
runs.append([lst[i - 1]])
58+
new_run.append(lst[i])
5859
else:
5960
runs.append(new_run)
6061
new_run = []
6162
else:
62-
new_run.append(the_array[i])
63-
for each in runs:
64-
sorted_runs.append(insertion_sort(each))
65-
sorted_array = []
63+
new_run.append(lst[i])
64+
65+
for run in runs:
66+
sorted_runs.append(insertion_sort(run))
67+
6668
for run in sorted_runs:
6769
sorted_array = merge(sorted_array, run)
68-
print(sorted_array)
70+
71+
return sorted_array
72+
73+
74+
def main():
75+
76+
lst = [5,9,10,3,-4,5,178,92,46,-18,0,7]
77+
sorted_lst = timsort(lst)
78+
print(sorted_lst)
79+
80+
if __name__ == '__main__':
81+
main()

0 commit comments

Comments
 (0)