-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
298 lines (244 loc) · 6.03 KB
/
main.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
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
import random
from ArrayStack import ArrayStack
from ListStack import ListStack
from ArrayQueue import ArrayQueue
from ListQueue import ListQueue
from NodeList import NodeList
from ArraySet import ArraySet
from ListSet import ListSet
from ArrayOrderedSet import ArrayOrderedSet
from ListOrderedSet import ListOrderedSet
from BinaryHeapPriorityQueue import BinaryHeapPriorityQueue
from BinarySearchTreeMap import BinarySearchTreeMap
random.seed(69)
def print_arr(arr):
for i in range(len(arr)):
print(arr[i], " ", end="")
print()
def swap(arr, ind1, ind2):
temp = arr[ind1]
arr[ind1] = arr[ind2]
arr[ind2] = temp
def find_ind_min(arr):
ind_min = 0
val_min = arr[0]
for i in range(1,len(arr)):
if arr[i] < val_min:
val_min = arr[i]
ind_min = i
return ind_min
def selection_sort(arr):
for i in range(len(arr)):
ind_min = find_ind_min(arr[i:len(arr)]) + i
swap(arr, i, ind_min)
def insertion_sort(arr):
ind_last_sorted = 0
for i in range(1, len(arr)):
count = 0
for j in range(ind_last_sorted, -1, -1): # goes from ind_last_sorted -> 0
ind_new = i - (ind_last_sorted - j)
if arr[ind_new] < arr[j]:
swap(arr, j, ind_new)
count += 1
else:
continue
ind_last_sorted += 1
def bubble_sort(arr):
ind_last_sorted = len(arr)
is_sorted = False
while not is_sorted:
is_sorted = True
for i in range(ind_last_sorted - 1):
if arr[i] > arr[i+1]:
swap(arr, i, i+1)
is_sorted = False
ind_last_sorted -= 1
def merge(arr, left, right):
ind_left = 0
ind_right = 0
ind_arr = 0
while ind_left < len(left) and ind_right < len(right):
if left[ind_left] < right[ind_right]:
arr[ind_arr] = left[ind_left]
ind_left += 1
else:
arr[ind_arr] = right[ind_right]
ind_right += 1
ind_arr += 1
while ind_left < len(left):
arr[ind_arr] = left[ind_left]
ind_left += 1
ind_arr += 1
while ind_right < len(right):
arr[ind_arr] = right[ind_right]
ind_right += 1
ind_arr += 1
def merge_sort(arr):
size = len(arr)
if size < 2:
return
mid = size//2
left = arr[0:mid]
right = arr[mid:size]
merge_sort(left)
merge_sort(right)
merge(arr, left, right)
def partition(arr, ind_low, ind_high):
ind_pivot = ind_high
val_pivot = arr[ind_pivot]
ind_partition = ind_low
for i in range(ind_low, ind_high): # from ind_low -> ind_high - 1 (don't want to reach ind_pivot = ind_high)
if arr[i] < val_pivot:
swap(arr, ind_partition, i)
ind_partition += 1
swap(arr, ind_partition, ind_pivot)
return ind_partition
def partition_rand(arr, ind_low, ind_high):
ind_pivot = random.randint(ind_low, ind_high+1)
swap(arr, ind_high, ind_pivot)
return partition(arr, ind_low, ind_high)
def quick_sort_helper(arr, ind_low, ind_high):
if ind_low < ind_high:
ind_partition = partition_rand(arr, ind_low, ind_high)
quick_sort_helper(arr, ind_low, ind_partition-1)
quick_sort_helper(arr, ind_partition+1, ind_high)
def quick_sort(arr):
quick_sort_helper(arr, 0, len(arr)-1)
def binary_search(arr, val):
ind_left = 0
ind_right = len(arr) - 1
while (ind_left <= ind_right):
ind_mid = (ind_left + ind_right) // 2
if val > arr[ind_mid]:
ind_left = ind_mid + 1
elif val < arr[ind_mid]:
ind_right = ind_mid - 1
else:
return ind_mid
return -1
def test_sort():
test = [5, 27, 1, 7, -1, 2, 27, 9, 7, 3, 6]
print_arr(test)
quick_sort(test)
print_arr(test)
print(binary_search(test, 5))
def test_ArrayStack():
s = ArrayStack()
print(s.empty())
for i in range(33):
s.push(i)
print('loop', i)
print(s.top())
s.print()
def test_ListStack():
s = ListStack()
print(s.empty())
s.top()
s.pop()
s.push(1)
s.push(2)
s.push(3)
print(s.top())
s.pop()
print(s.top())
def test_ArrayQueue():
s = ArrayQueue()
print(s.empty())
for i in range(33):
s.enqueue(i)
print('loop', i)
print(s.front())
s.print()
def test_ListQueue():
s = ListQueue()
print(s.empty())
s.enqueue(1)
s.enqueue(2)
s.enqueue(3)
print(s.front())
s.dequeue()
print(s.front())
s.dequeue()
print(s.front())
s.dequeue()
print(s.front())
def test_NodeList():
s = NodeList()
print(s.empty())
s.insert(0, 69)
s.print()
s.insert_front(3)
s.insert_front(4)
s.insert_front(5)
s.insert_back(2)
s.insert_back(1)
s.print()
s.remove(3)
s.print()
def test_ArraySet():
s = ArraySet()
s.insert(1)
s.insert(2)
s.insert(3)
s.insert(2)
s.insert(1)
s.insert(4)
s.remove(1)
s.remove(0)
s.remove(3)
s.print()
def test_ListSet():
s = ListSet()
s.insert(1)
s.insert(5)
s.insert(2)
s.remove(3)
s.remove(2)
s.print()
def test_ArrayOrderedSet():
s = ArrayOrderedSet()
s.insert(5)
s.insert(1)
s.insert(3)
s.insert(8)
s.insert(2)
s.remove(2)
s.print()
def test_ListOrderedSet():
s = ListOrderedSet()
s.print()
s.insert(2)
s.insert(7)
s.insert(1)
s.insert(69)
s.insert(42)
s.insert(0)
s.insert(6)
s.insert(9)
s.print()
def test_BinaryHeapPriorityQueue():
s = BinaryHeapPriorityQueue()
print(s.empty())
s.insert(5)
s.insert(1)
s.insert(9)
s.insert(3)
s.insert(7)
s.insert(6)
s.remove()
s.print()
def test_BinarySearchTreeMap():
s = BinarySearchTreeMap()
s.insert(7, 5)
s.insert(1, 2)
s.insert(3, 4)
s.insert(9, 2)
s.insert(6, 1)
s.insert(4, 0)
s.insert(2, 3)
s.print()
s.remove(3)
s.remove(6)
s.print()
if __name__ == '__main__':
test_BinarySearchTreeMap()