Skip to content

Commit eeb8cb1

Browse files
authored
Create TimSort.py
1 parent 13fbd04 commit eeb8cb1

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

TimSort.py

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Python3 program to perform basic timSort
2+
MIN_MERGE = 32
3+
4+
5+
def calcMinRun(n):
6+
"""Returns the minimum length of a
7+
run from 23 - 64 so that
8+
the len(array)/minrun is less than or
9+
equal to a power of 2.
10+
11+
e.g. 1=>1, ..., 63=>63, 64=>32, 65=>33,
12+
..., 127=>64, 128=>32, ...
13+
"""
14+
r = 0
15+
while n >= MIN_MERGE:
16+
r |= n & 1
17+
n >>= 1
18+
return n + r
19+
20+
21+
# This function sorts array from left index to
22+
# to right index which is of size atmost RUN
23+
def insertionSort(arr, left, right):
24+
for i in range(left + 1, right + 1):
25+
j = i
26+
while j > left and arr[j] < arr[j - 1]:
27+
arr[j], arr[j - 1] = arr[j - 1], arr[j]
28+
j -= 1
29+
30+
31+
# Merge function merges the sorted runs
32+
def merge(arr, l, m, r):
33+
34+
# original array is broken in two parts
35+
# left and right array
36+
len1, len2 = m - l + 1, r - m
37+
left, right = [], []
38+
for i in range(0, len1):
39+
left.append(arr[l + i])
40+
for i in range(0, len2):
41+
right.append(arr[m + 1 + i])
42+
43+
i, j, k = 0, 0, l
44+
45+
# after comparing, we merge those two array
46+
# in larger sub array
47+
while i < len1 and j < len2:
48+
if left[i] <= right[j]:
49+
arr[k] = left[i]
50+
i += 1
51+
52+
else:
53+
arr[k] = right[j]
54+
j += 1
55+
56+
k += 1
57+
58+
# Copy remaining elements of left, if any
59+
while i < len1:
60+
arr[k] = left[i]
61+
k += 1
62+
i += 1
63+
64+
# Copy remaining element of right, if any
65+
while j < len2:
66+
arr[k] = right[j]
67+
k += 1
68+
j += 1
69+
70+
71+
# Iterative Timsort function to sort the
72+
# array[0...n-1] (similar to merge sort)
73+
def timSort(arr):
74+
n = len(arr)
75+
minRun = calcMinRun(n)
76+
77+
# Sort individual subarrays of size RUN
78+
for start in range(0, n, minRun):
79+
end = min(start + minRun - 1, n - 1)
80+
insertionSort(arr, start, end)
81+
82+
# Start merging from size RUN (or 32). It will merge
83+
# to form size 64, then 128, 256 and so on ....
84+
size = minRun
85+
while size < n:
86+
87+
# Pick starting point of left sub array. We
88+
# are going to merge arr[left..left+size-1]
89+
# and arr[left+size, left+2*size-1]
90+
# After every merge, we increase left by 2*size
91+
for left in range(0, n, 2 * size):
92+
93+
# Find ending point of left sub array
94+
# mid+1 is starting point of right sub array
95+
mid = min(n - 1, left + size - 1)
96+
right = min((left + 2 * size - 1), (n - 1))
97+
98+
# Merge sub array arr[left.....mid] &
99+
# arr[mid+1....right]
100+
if mid < right:
101+
merge(arr, left, mid, right)
102+
103+
size = 2 * size
104+
105+
106+
# Driver program to test above function
107+
if __name__ == "__main__":
108+
109+
arr = [-2, 7, 15, -14, 0, 15, 0,
110+
7, -7, -4, -13, 5, 8, -14, 12]
111+
112+
print("Given Array is")
113+
print(arr)
114+
115+
# Function Call
116+
timSort(arr)
117+
118+
print("After Sorting Array is")
119+
print(arr)
120+

0 commit comments

Comments
 (0)