-
Notifications
You must be signed in to change notification settings - Fork 7
/
Sorting_Algorithms.py
280 lines (250 loc) · 9.6 KB
/
Sorting_Algorithms.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
import pygame
import os, sys
import copy
import random
from pygame.locals import *
from GUI_Functions import KEY_PRESSED
from Exceptions import ArraySizeError
def InsertionSort(MyArray, Win, Font, DrawSort=True):
for arrayPosition in range(1, len(MyArray.Array)):
actualElement = MyArray.Array[arrayPosition] # Elemento que se compara.
while arrayPosition > 0 and MyArray.Array[arrayPosition - 1] > actualElement:
MyArray.Array[arrayPosition] = MyArray.Array[arrayPosition - 1]
arrayPosition -= 1
MyArray.Array[arrayPosition] = actualElement
if DrawSort:
MyArray.Moving_Elements = [MyArray.Array[arrayPosition]]
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
MyArray.Draw(Win, Font)
if not MyArray.isSorted:
MyArray.Draw_Check_Sorted(Win, Font)
MyArray.isSorted = True
return MyArray
def CocktailShakerSort(MyArray, Win, Font, DrawSort=True):
for i in range(len(MyArray.Array) - 1, 0, -1):
Swapped = False
for j in range(i, 0, -1):
if MyArray.Array[j] < MyArray.Array[j - 1]:
MyArray.Array[j], MyArray.Array[j - 1] = MyArray.Array[j - 1], MyArray.Array[j]
MyArray.Moving_Elements = [MyArray.Array[j], MyArray.Array[j - 1]]
if DrawSort:
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
MyArray.Draw(Win, Font, extraAcceses=2) # Since we are moving two elements on the array, we have to add 2 acceses instead of one.
Swapped = True
for j in range(i):
if MyArray.Array[j] > MyArray.Array[j + 1]:
MyArray.Array[j], MyArray.Array[j + 1] = MyArray.Array[j + 1], MyArray.Array[j]
MyArray.Moving_Elements = [MyArray.Array[j], MyArray.Array[j + 1]]
if DrawSort:
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
MyArray.Draw(Win, Font, extraAcceses=2) # Since we are moving two elements on the array, we have to add 2 acceses instead of one.
Swapped = True
if not Swapped:
if not MyArray.isSorted:
MyArray.Draw_Check_Sorted(Win, Font)
MyArray.isSorted = True
return MyArray
def BubbleSort(MyArray, Win, Font, DrawSort=True):
Swapped = True
while Swapped:
Swapped = False
for i in range(len(MyArray.Array) - 1):
if MyArray.Array[i] > MyArray.Array[i + 1]:
MyArray.Array[i], MyArray.Array[i + 1] = MyArray.Array[i + 1], MyArray.Array[i]
MyArray.Moving_Elements = [MyArray.Array[i], MyArray.Array[i + 1]]
if DrawSort:
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
MyArray.Draw(Win, Font, extraAcceses=2) # Since we are moving two elements on the array, we have to add 2 acceses instead of one.
Swapped = True
if not MyArray.isSorted:
MyArray.Draw_Check_Sorted(Win, Font)
MyArray.isSorted = True
return MyArray
RUN = 32
def Tim_InsertionSort(MyArray, Left, Right, Win, Font, DrawSort):
for i in range(Left + 1, Right + 1):
Temp = MyArray.Array[i]
j = i - 1
while MyArray.Array[j] > Temp and j >= Left:
MyArray.Array[j + 1] = MyArray.Array[j]
MyArray.Moving_Elements = [MyArray.Array[j], MyArray.Array[j + 1]]
j -= 1
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
if DrawSort:
MyArray.Draw(Win, Font)
MyArray.Array[j + 1] = Temp
MyArray.Moving_Elements = [MyArray.Array[j + 1]]
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
if DrawSort:
MyArray.Draw(Win, Font)
def Merge(MyArray, l, m, r, Win, Font, DrawSort):
LEN_1, LEN_2 = m - l + 1, r - m
Left, Right = [], []
for i in range(0, LEN_1):
Left.append(MyArray.Array[l + i])
for i in range(0, LEN_2):
Right.append(MyArray.Array[m + 1 + i])
i, j, k = 0, 0, l
while i < LEN_1 and j < LEN_2:
if Left[i] <= Right[j]:
MyArray.Array[k] = Left[i]
MyArray.Moving_Elements = [MyArray.Array[k]]
i += 1
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
MyArray.Draw(Win, Font)
else:
MyArray.Array[k] = Right[j]
MyArray.Moving_Elements = [MyArray.Array[k]]
j += 1
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
if DrawSort:
MyArray.Draw(Win, Font)
k += 1
while i < LEN_1:
MyArray.Array[k] = Left[i]
MyArray.Moving_Elements = [MyArray.Array[k]]
k += 1
i += 1
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
if DrawSort:
MyArray.Draw(Win, Font)
while j < LEN_2:
MyArray.Array[k] = Right[j]
MyArray.Moving_Elements = [MyArray.Array[k]]
k += 1
j += 1
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
if DrawSort:
MyArray.Draw(Win, Font)
def TimSort(MyArray, Win, Font, DrawSort=True):
if not MyArray.isSorted:
N = len(MyArray.Array)
for i in range(0, N, RUN):
Tim_InsertionSort(MyArray, i, min((i + 31), (N - 1)), Win, Font, DrawSort)
Size = RUN
while Size < N:
for Left in range(0, N, 2 * Size):
Mid = Left + Size - 1
Right = min((Left + 2 * Size - 1), (N - 1))
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
if DrawSort:
MyArray.Draw(Win, Font)
Merge(MyArray, Left, Mid, Right, Win, Font, DrawSort)
Size = 2 * Size
if not MyArray.isSorted:
MyArray.Draw_Check_Sorted(Win, Font)
MyArray.isSorted = True
return MyArray
def CycleSort(MyArray, Win, Font):
if not MyArray.isSorted:
Writes = 0
for cycleStart in range(0, len(MyArray) - 1):
Item = MyArray.Array[cycleStart]
Position = cycleStart
for i in range(cycleStart + 1, len(MyArray)):
if MyArray.Array[i] < Item:
Position += 1
if Position == cycleStart:
continue
while Item == MyArray.Array[Position]:
Position += 1
MyArray.Array[Position], Item = Item, MyArray.Array[Position]
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
MyArray.Draw(Win, Font, extraAcceses=2)
Writes += 1
while Position != cycleStart:
Position = cycleStart
for i in range(cycleStart + 1, len(MyArray)):
if MyArray.Array[i] < Item:
Position += 1
while Item == MyArray.Array[Position]:
Position += 1
MyArray.Array[Position], Item = Item, MyArray.Array[Position]
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
MyArray.Draw(Win, Font, extraAcceses=2)
Writes += 1
if not MyArray.isSorted:
MyArray.Draw_Check_Sorted(Win, Font)
MyArray.isSorted = True
return MyArray
def rotLeft(Array, n=1):
return Array[n % len(Array):] + Array[:n % len(Array)]
def Rotate(MyArray, rotationTimes, Win, Font):
if not MyArray.isSorted:
for _ in range(rotationTimes):
MyArray.Array = rotLeft(MyArray.Array)
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
MyArray.Draw(Win, Font)
MyArray.Draw_Check_Sorted(Win, Font)
MyArray.isSorted = True
return MyArray
def ElementsIncreaser(MyArray, increaseSize, Win, Font):
if not MyArray.isSorted:
for _ in range(increaseSize):
for arrayPosition in range(len(MyArray)):
MyArray.Array[arrayPosition] += 1
KEY = KEY_PRESSED()
if KEY == "QUIT":
pygame.quit()
sys.exit()
else:
MyArray.Draw(Win, Font)
MyArray.Draw_Check_Sorted(Win, Font)
MyArray.isSorted = True
return MyArray