Repository navigation
Expand file tree
/
Copy pathHeapMergeSort.py
More file actions
239 lines (177 loc) · 5.46 KB
/
Copy pathHeapMergeSort.py
File metadata and controls
239 lines (177 loc) · 5.46 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
import pygame,random,time
from math import log
from algorithms.colors import *
from algorithms.data_struct.priority_queue import Heap
color_list = [
Dark_red ,
Blue ,
deepskyblue ,
Yellow ,
lightsteelblue,
Lime,
Dark_yellow ,
Flame ,
Cyan ,
Magenta ,
Gray ,
Dark_gray ,
Maroon ,
skyblue ,
Olive ,
dodgerblue ,
Purple ,
cornflowerblue,
Teal ,
steelblue ,
Navy ,
cadetblue ,
Castanho ,
mediumslateblue,
Orange ,
Springgreen ,
royalblue ,
]
t = 0
pygame.init()
screen_height = 700
screen_width = 1300
screen = pygame.display.set_mode((screen_width,screen_height))
screen.fill((0,0,0))
square_width = 3
space = 2
index_color = (0,200,0)
index2_color = (200,200,0)
numb_color = (50,50,250)
const_color = (255,255,255)
sum_color = (250,250,0)
duplicate_color = (255,0,0)
ground = 500
size_rate = 2
rect_color = []
def print_rect(screen,arr,i,color):
if i >= len(arr): return
pygame.draw.rect( screen , color , (10 + (square_width+space)*i , ground - size_rate*arr[i], square_width , size_rate*arr[i]) )
pygame.display.update()
time.sleep(0.005)
def display(screen,arr,pivot_pos = None):
pygame.draw.rect(screen,Black,(0,100,screen_width-150,screen_height))
for i in range(len(arr)):
pygame.draw.rect( screen , rect_color[i] , (10 + (square_width+space)*i , ground - size_rate*arr[i], square_width , size_rate*arr[i]) )
if pivot_pos is not None:
i = pivot_pos
pygame.draw.rect( screen , rect_color[i] , (10 + (square_width+space)*i , ground - size_rate*arr[i], square_width , size_rate*arr[i]) )
pygame.display.update()
def show_Heap(H):
pygame.draw.rect(screen, Light_grey, (1150,145+100 ,80, 60)) # erase
font = pygame.font.Font('freesansbold.ttf',20)
text = font.render(str(H.__len__()) ,True, Black)
screen.blit(text,text.get_rect(center = (1190,180+100)))
pygame.display.update()
time.sleep( 0.015*log(H.__len__()+1,2) )
def print_range(i,f,color):
global t
for index in range(i,f+1):
rect_color[index] = color
print_rect(screen,arr,index,color)
t += 1
if t == len(color_list):t = 0
def HeapMerge(arr,runs):
H = Heap(comp = lambda a,b: a > b)
font = pygame.font.Font('freesansbold.ttf',18)
text = font.render( 'Heap size',True, Light_grey)
screen.blit(text,text.get_rect(center = (1195,180+50)))
show_Heap(H)
time.sleep(1)
for i,f in runs:
H.insert((arr[i],i,f))
show_Heap(H)
print_rect(screen,arr,i,Black)
aux_arr = list( 0 for _ in range(len(arr)))
aux = 0
for _ in range(len(arr)):
# coloca na Heap
aux_arr[aux],index,last = H.pop()
print_rect(screen,aux_arr,aux,White)
print_rect(screen,arr,index,Light_grey)
show_Heap(H)
print_rect(screen,arr,index,Black)
if index <= aux:
print_rect(screen,aux_arr,index,White)
aux += 1
# se o elemento tirado for o ultimo nao coloca o prox na Heap
if index < last:
H.insert((arr[index+1],index+1,last))
show_Heap(H)
arr[:] = aux_arr[:]
display(screen,arr)
def reverse(arr,start,end):
global t
i = start
f = end
while i < f:
arr[i],arr[f] = arr[f],arr[i]
i += 1
f -= 1
display(screen,arr)
print_range(i,f,Red)
time.sleep(0.05)
print_range(i,f,color_list[t])
def Sort(arr):
global t
font = pygame.font.Font('freesansbold.ttf',30)
text = font.render("HeapMerge Sort. ",True,Light_grey)
screen.blit(text,text.get_rect(center = (400,30)))
last = len(arr) -1
i = 0
f = 0
runs = []
while f < len(arr):
#ascending
if f < len(arr)-1 and arr[f] <= arr[f+1]:
while f < last and arr[f] <= arr[f+1]:
f += 1
if i <= f:
runs.append((i,f))
print_range(i,f,color_list[t])
display(screen,arr)
f += 1
i = f
if f == len(arr):break
#descending
while f < last and arr[f] >= arr[f+1]:
f += 1
i0,f0 = i,f
reverse(arr,i0,f0)
if i <= f:
if f < last and arr[f] > arr[f+1]:
runs.append((i,f))
print_range(i,f,color_list[t])
elif f == last:
runs.append((i,f))
print_range(i,f,color_list[t])
else:
continue
f += 1
i = f
display(screen,arr)
time.sleep(1.5)
HeapMerge(arr, runs)
if __name__ == "__main__":
N = 200
arr = list(random.randint(5,200) for _ in range(N))
rect_color = list(White for _ in range(N))
display(screen,arr)
time.sleep(1)
Sort(arr)
print(arr)
f, last = 0,N-1
while f < last and arr[f] <= arr[f+1]:
f += 1
print(f,"is sorted:",f == last)
running = True
while running :
# pygame stuff:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() #exit pygame,
running = False #exit() program