-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.py
More file actions
174 lines (136 loc) · 4.62 KB
/
sorting.py
File metadata and controls
174 lines (136 loc) · 4.62 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from sortable_array import SortableArray
from collections.abc import Generator
def noop() -> Generator[None, None, None]:
yield
def bubble_sort(array: SortableArray) -> Generator[None, None, None]:
for i in range(len(array)):
for j in range(len(array) - 1 - i):
if array[j] > array[j + 1]:
array.swap((j, j + 1))
yield
def insertion_sort(array: SortableArray) -> Generator[None, None, None]:
for i in range(1, len(array)):
key = array[i]
j = i - 1
while j >= 0 and array[j] > key:
array[j + 1] = array[j]
j -= 1
array[j + 1] = key
yield
def quick_sort(array: SortableArray) -> Generator[None, None, None]:
stack = [(0, len(array) - 1)] # Start with the whole array on the stack
while stack:
low, high = stack.pop() # Get the current subarray bounds
if low >= high:
continue
pivot = array[(low + high) // 2]
i, j = low, high
while i <= j:
# Move i to the right until finding an element >= pivot
while array[i] < pivot:
i += 1
# Move j to the left until finding an element <= pivot
while array[j] > pivot:
j -= 1
# Swap elements at i and j if i <= j
if i <= j:
array.swap((i, j))
i += 1
j -= 1
yield # Yield to allow the caller to pause
# Push both subarrays onto the stack for further sorting
if low < j:
stack.append((low, j)) # Left half
if i < high:
stack.append((i, high)) # Right half
def heap_sort(array: SortableArray) -> Generator[None, None, None]:
n = len(array)
def heapify(end: int, i: int) -> None:
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < end and array[left] > array[largest]:
largest = left
if right < end and array[right] > array[largest]:
largest = right
if largest != i:
array.swap((i, largest))
heapify(end, largest)
# Build max heap
for i in range(n // 2 - 1, -1, -1):
heapify(n, i)
yield
# One by one extract elements from heap
for i in range(n - 1, 0, -1):
array.swap((0, i)) # Move current root to end
heapify(i, 0)
yield
def merge_sort(array: SortableArray) -> Generator[None, None, None]:
def merge(left: int, mid: int, right: int) -> Generator:
left_half = array[left:mid + 1]
right_half = array[mid + 1:right + 1]
i = j = 0
k = left
while i < len(left_half) and j < len(right_half):
if left_half[i] <= right_half[j]:
array[k] = left_half[i]
i += 1
else:
array[k] = right_half[j]
j += 1
k += 1
yield
while i < len(left_half):
array[k] = left_half[i]
i += 1
k += 1
yield
while j < len(right_half):
array[k] = right_half[j]
j += 1
k += 1
yield
def sort(left: int, right: int) -> Generator:
if left < right:
mid = (left + right) // 2
yield from sort(left, mid)
yield from sort(mid + 1, right)
yield from merge(left, mid, right)
yield from sort(0, len(array) - 1)
def radix_sort(array: SortableArray) -> Generator[None, None, None]:
def sort(array: SortableArray, exp: int) -> Generator[None, None, None]:
n = len(array)
output = [0] * n
count = [0] * 10 # Base 10
for i in range(n):
index = array[i] // exp
count[index % 10] += 1
for i in range(1, 10):
count[i] += count[i - 1]
for i in range(n - 1, -1, -1):
index = array[i] // exp
output[count[index % 10] - 1] = array[i]
count[index % 10] -= 1
yield
for i in range(n):
array[i] = output[i]
yield
max_value = max(array)
exp = 1
while max_value // exp > 0:
yield from sort(array, exp)
exp *= 10
def shell_sort(array: SortableArray) -> Generator[None, None, None]:
n = len(array)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = array[i]
j = i
while j >= gap and array[j - gap] > temp:
array[j] = array[j - gap]
j -= gap
yield
array[j] = temp
yield
gap //= 2