-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
307 lines (259 loc) · 9.53 KB
/
algorithms.py
File metadata and controls
307 lines (259 loc) · 9.53 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
All of the following functions are generator functions.
They are used because they can print the process step-by-step, unlike traditional functions which take an input and show an output.
"""
def quicksort_generator(arr):
"""
Quicksort implementation that yields the results, instead of simply returning a value.
This way we can use it inside the canvas containing the visualizer
"""
step_counter = [0] # Using list to maintain reference across recursive calls
def quicksort_recursive(arr, low, high, step_counter):
if low < high:
# Partition and get the generator and final pivot index
partition_gen = partition(arr, low, high, step_counter)
pivot_index = None
for state in partition_gen:
if 'pivot_index' in state:
pivot_index = state['pivot_index']
else:
yield state
# Recursively sort left and right subarrays
if pivot_index is not None:
yield from quicksort_recursive(arr, low, pivot_index - 1, step_counter)
yield from quicksort_recursive(arr, pivot_index + 1, high, step_counter)
def partition(arr, low, high, step_counter):
pivot = arr[high]
i = low - 1
step_counter[0] += 1
highlights = {high: 'pivot'}
for j in range(low, high):
highlights[j] = 'partitioning'
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Partitioning: Pivot = {pivot} at index {high}',
'step': step_counter[0]
}
for j in range(low, high):
step_counter[0] += 1
highlights = {high: 'pivot', j: 'comparing'}
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Comparing {arr[j]} with pivot {pivot}',
'step': step_counter[0]
}
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
if i != j:
step_counter[0] += 1
highlights = {high: 'pivot', i: 'comparing', j: 'comparing'}
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Swapped {arr[j]} and {arr[i]}',
'step': step_counter[0]
}
# Place pivot in correct position
arr[i + 1], arr[high] = arr[high], arr[i + 1]
step_counter[0] += 1
highlights = {}
for k in range(low, i + 1):
highlights[k] = 'partitioning'
highlights[i + 1] = 'pivot'
for k in range(i + 2, high + 1):
highlights[k] = 'partitioning'
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Pivot {pivot} placed at position {i + 1}',
'step': step_counter[0]
}
# Yield the final pivot index
yield {'pivot_index': i + 1}
# Start the sorting process
yield from quicksort_recursive(arr, 0, len(arr) - 1, step_counter)
# Final sorted state
step_counter[0] += 1
highlights = {i: 'sorted' for i in range(len(arr))}
yield {
'array': arr[:],
'highlights': highlights,
'description': 'Sorting complete!',
'step': step_counter[0],
'sorted': True
}
def mergesort_generator(arr):
"""
Mergesort implementation that yields states for visualization
"""
step_counter = [0]
def mergesort_recursive(arr, left, right, step_counter):
if left < right:
mid = (left + right) // 2
step_counter[0] += 1
highlights = {}
for i in range(left, mid + 1):
highlights[i] = 'merging'
for i in range(mid + 1, right + 1):
highlights[i] = 'partitioning'
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Dividing array: [{left}:{mid}] | [{mid+1}:{right}]',
'step': step_counter[0]
}
# Sort left half
yield from mergesort_recursive(arr, left, mid, step_counter)
# Sort right half
yield from mergesort_recursive(arr, mid + 1, right, step_counter)
# Merge the sorted halves
yield from merge(arr, left, mid, right, step_counter)
def merge(arr, left, mid, right, step_counter):
"""Merge two sorted subarrays"""
# Create temporary arrays
left_arr = arr[left:mid + 1]
right_arr = arr[mid + 1:right + 1]
step_counter[0] += 1
highlights = {}
for i in range(left, mid + 1):
highlights[i] = 'merging'
for i in range(mid + 1, right + 1):
highlights[i] = 'partitioning'
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Merging subarrays [{left}:{mid}] and [{mid+1}:{right}]',
'step': step_counter[0]
}
# Merge the temp arrays back
i = j = 0
k = left
while i < len(left_arr) and j < len(right_arr):
step_counter[0] += 1
highlights = {left + i: 'comparing', mid + 1 + j: 'comparing'}
for m in range(left, k):
highlights[m] = 'sorted'
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Comparing {left_arr[i]} and {right_arr[j]}',
'step': step_counter[0]
}
if left_arr[i] <= right_arr[j]:
arr[k] = left_arr[i]
i += 1
else:
arr[k] = right_arr[j]
j += 1
k += 1
step_counter[0] += 1
highlights = {}
for m in range(left, k):
highlights[m] = 'sorted'
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Placed {arr[k-1]} at position {k-1}',
'step': step_counter[0]
}
# Copy remaining elements
while i < len(left_arr):
arr[k] = left_arr[i]
i += 1
k += 1
step_counter[0] += 1
highlights = {}
for m in range(left, k):
highlights[m] = 'sorted'
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Placed remaining {arr[k-1]} at position {k-1}',
'step': step_counter[0]
}
while j < len(right_arr):
arr[k] = right_arr[j]
j += 1
k += 1
step_counter[0] += 1
highlights = {}
for m in range(left, k):
highlights[m] = 'sorted'
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Placed remaining {arr[k-1]} at position {k-1}',
'step': step_counter[0]
}
# Show merged result
step_counter[0] += 1
highlights = {}
for m in range(left, right + 1):
highlights[m] = 'sorted'
yield {
'array': arr[:],
'highlights': highlights,
'description': f'Merged subarray [{left}:{right}]',
'step': step_counter[0]
}
# Start the sorting process
yield from mergesort_recursive(arr, 0, len(arr) - 1, step_counter)
# Final sorted state
step_counter[0] += 1
highlights = {i: 'sorted' for i in range(len(arr))}
yield {
'array': arr[:],
'highlights': highlights,
'description': 'Sorting complete!',
'step': step_counter[0],
'sorted': True
}
def binary_search_generator(arr, target):
low = 0
high = len(arr) - 1
step = 0
while low <= high:
mid = (low + high) // 2
step += 1
yield {
'low': low,
'high': high,
'mid': mid,
'description': f'Searching range [{low}:{high}], mid={mid}, value={arr[mid]}',
'step': step
}
if arr[mid] == target:
step += 1
yield {
'found_index': mid,
'description': f'Found {target} at index {mid}!',
'step': step
}
return
elif arr[mid] < target:
low = mid + 1
step += 1
yield {
'low': low,
'high': high,
'description': f'{arr[mid]} < {target}, searching right half',
'step': step
}
else:
high = mid - 1
step += 1
yield {
'low': low,
'high': high,
'description': f'{arr[mid]} > {target}, searching left half',
'step': step
}
# Not found
step += 1
yield {
'description': f'{target} not found in the array',
'step': step
}