-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
126 lines (113 loc) · 5.14 KB
/
algorithms.py
File metadata and controls
126 lines (113 loc) · 5.14 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
def bubble_sort(arr, canvas, speed):
import time
import visualizer
for i in range(len(arr)-1, 0, -1):
# For each iteration through the unsorted parts of the array:
for j in range(i):
# For each element in that iteration:
if arr[j] > arr[j+1]: # If the value of the current element is greater than the next element.
arr[j], arr[j+1] = arr[j+1], arr[j] # Swap the elements.
time.sleep(speed) # Make sure speed is an integer.
color_array = [
'green' if x > i else ('red' if x == j or x == j+1 else 'black')
for x in range(len(arr))
]
visualizer.draw_array(canvas, arr, color_array)
print("Sorted array:", arr) # Print the sorted array for debugging purposes.
return arr
# Return the sorted array.
def merge_sort(array, start, end, canvas, speed):
if start < end:
mid = (start + end) // 2
merge_sort(array, start, mid, canvas, speed)
merge_sort(array, mid + 1, end, canvas, speed)
merge(array, start, mid, end, canvas, speed)
# After all recursive calls and merging are done, print and return the sorted array.
if start == 0 and end == len(array) - 1:
print("Sorted array:", array)
return array
def merge(array, start, mid, end, canvas, speed):
import time
import visualizer
left = array[start:mid+1] # Left is the left half of the array.
right = array[mid+1:end+1] # Right is the right half of the array.
i = j = 0 # i and j are both leftmost elements.
k = start
while i < len(left) and j < len(right): # While there are still elements in both lists:
if left[i] <= right[j]: # If the leftmost right element is more than the leftmost left element:
array[k] = left[i] # Add the element to the merged array.
# Animation:
time.sleep(speed)
color_array = [
# The bar at index k is colored green (currently being merged), bars at index i or j+1 are colored red, all others are black.
'green' if x == k else ('red' if x == i or x == j+1 else 'black')
for x in range(len(array))
]
visualizer.draw_array(canvas, array, color_array)
i += 1
else: # If the leftmost left element is more than the leftmost right element:
array[k] = right[j]
time.sleep(speed)
color_array = [
# The bar at index k is colored green (currently being merged), bars at index i or j+1 are colored red, all others are black.
'green' if x == k else ('red' if x == i or x == j+1 else 'black')
for x in range(len(array))
]
visualizer.draw_array(canvas, array, color_array)
j += 1
k += 1
while i < len(left):
array[k] = left[i]
time.sleep(speed)
color_array = [
# The bar at index k is colored green (currently being merged), bars at index i or j+1 are colored red, all others are black.
'green' if x == k else ('red' if x == i or x == j+1 else 'black')
for x in range(len(array))
]
visualizer.draw_array(canvas, array, color_array)
i += 1
k += 1
while j < len(right):
array[k] = right[j]
time.sleep(speed)
color_array = [
'green' if x == k else 'black'
for x in range(len(array))
]
visualizer.draw_array(canvas, array, color_array)
j += 1
k += 1
def quick_sort(array, low, high, canvas, speed):
if low < high:
pi = partition(array, low, high, canvas, speed)
quick_sort(array, low, pi - 1, canvas, speed)
quick_sort(array, pi + 1, high, canvas, speed)
# After sorting, print and return the sorted array if it's the top-level call:
if low == 0 and high == len(array) - 1: # In the end the element with the highest value the index
print("Sorted array:", array) # should be the length of the array minus one and the
return array # lowest value should be index 0.
def partition(array, low, high, canvas, speed):
import time
import visualizer
pivot = array[high]
i = low - 1
for j in range(low, high):
if array[j] < pivot:
i += 1
array[i], array[j] = array[j], array[i]
time.sleep(speed)
color_array = [
'green' if x == i or x == j else ('red' if x == high else 'black')
for x in range(len(array))
]
visualizer.draw_array(canvas, array, color_array)
array[i + 1], array[high] = array[high], array[i + 1]
time.sleep(speed)
color_array = [
'green' if x == i + 1 or x == high else 'black'
for x in range(len(array))
]
visualizer.draw_array(canvas, array, color_array)
return i + 1
import main
array = main.on_generate_array_click()