-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathtim_sort.py
More file actions
117 lines (93 loc) · 2.84 KB
/
tim_sort.py
File metadata and controls
117 lines (93 loc) · 2.84 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from typing import Protocol
class Comparable(Protocol):
def __lt__(self, other: object) -> bool: ...
def __le__(self, other: object) -> bool: ...
def binary_search[T: Comparable](arr: list[T], item: T, left: int, right: int) -> int:
"""
Return the index where `item` should be inserted in `arr[left:right+1]`
to keep it sorted.
>>> binary_search([1, 3, 5, 7], 6, 0, 3)
3
>>> binary_search([1, 3, 5, 7], 0, 0, 3)
0
>>> binary_search([1, 3, 5, 7], 8, 0, 3)
4
"""
while left <= right:
mid = (left + right) // 2
if arr[mid] == item:
return mid
elif arr[mid] < item:
left = mid + 1
else:
right = mid - 1
return left
def insertion_sort[T: Comparable](arr: list[T]) -> list[T]:
"""
Sort the list in-place using binary insertion sort.
>>> insertion_sort([3, 1, 2, 4])
[1, 2, 3, 4]
"""
for i in range(1, len(arr)):
key = arr[i]
j = binary_search(arr, key, 0, i - 1)
arr[:] = [*arr[:j], key, *arr[j:i], *arr[i + 1 :]]
return arr
def merge[T: Comparable](left: list[T], right: list[T]) -> list[T]:
"""
Merge two sorted lists into one sorted list.
>>> merge([1, 3, 5], [2, 4, 6])
[1, 2, 3, 4, 5, 6]
"""
merged: list[T] = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merged.append(left[i])
i += 1
else:
merged.append(right[j])
j += 1
merged.extend(left[i:])
merged.extend(right[j:])
return merged
def tim_sort[T: Comparable](arr: list[T]) -> list[T]:
"""
Simplified version of TimSort for educational purposes.
TimSort is a hybrid stable sorting algorithm that combines merge sort
and insertion sort. It was originally designed by Tim Peters for Python (2002).
Source: https://en.wikipedia.org/wiki/Timsort
>>> tim_sort("Python")
['P', 'h', 'n', 'o', 't', 'y']
>>> tim_sort([5, 4, 3, 2, 1])
[1, 2, 3, 4, 5]
>>> tim_sort([3, 2, 1]) == sorted([3, 2, 1])
True
>>> tim_sort([]) # empty input
[]
"""
if not isinstance(arr, list):
arr = list(arr)
if not arr:
return []
min_run = 32
n = len(arr)
if n == 1:
return arr.copy()
runs: list[list[T]] = []
for start in range(0, n, min_run):
end = min(start + min_run, n)
run = insertion_sort(arr[start:end])
runs.append(run)
while len(runs) > 1:
new_runs: list[list[T]] = []
for i in range(0, len(runs), 2):
if i + 1 < len(runs):
new_runs.append(merge(runs[i], runs[i + 1]))
else:
new_runs.append(runs[i])
runs = new_runs
return runs[0] if runs else []
if __name__ == "__main__":
import doctest
doctest.testmod()