-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtim.py
45 lines (36 loc) · 1.02 KB
/
tim.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
def insertionSort(a, left = 0, right = None):
for i in range(left + 1, right + 1):
key = a[i]
j = i - 1
while j >= left and a[j] > key:
a[j + 1] = a[j]
j -= 1
a[j + 1] = key
return a
def merge(left, right):
if not left:
return right
if not right:
return left
if left[0] < right[0]:
return [left[0]] + merge(left[1:], right)
else:
return [right[0]] + merge(left, right[1:])
def timSort(a):
minRun = 2
n = len(a)
for i in range(0, n, minRun):
insertionSort(a, i, min(i + minRun - 1, (n-1)))
size = minRun
while size < n:
for start in range(0, n, size * 2):
mid = start + size
end = min((start + size * 2 - 1), (n - 1))
mergedA = merge(a[start:mid], a[mid:end + 1])
a[start:start + len(mergedA)] = mergedA
size *= 2
return a
a = [9, 3, 2, 5]
print(a)
timSort(a)
print(a)