-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectSort.py
More file actions
95 lines (85 loc) · 2.9 KB
/
SelectSort.py
File metadata and controls
95 lines (85 loc) · 2.9 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
import pygame
import random
import sys
"""Global variables"""
collor = ((255, 255, 255), (0, 0, 0))
"""Screen setup"""
resolution = (1300, 700)
windowSurface = pygame.display.set_mode(resolution, 0, 32)
pygame.display.set_caption("Waiting...")
windowSurface.fill(collor[0])
""""Screen update function"""
def update():
windowSurface.fill(collor[0])
for h in range(len(vetor)):
d = resolution[1] / max(vetor)
x0 = ((resolution[0] - 5) / (len(vetor) * 1.0)) * h + 2
x1 = ((resolution[0] - 5) / (len(vetor) * 1.0)) * (h + 1)
y0 = (resolution[1] - vetor[h] * d)
y1 = (resolution[1] - 1)
pygame.draw.polygon(windowSurface, collor[1], ((x0, y0), (x1, y0), (x1, y1), (x0, y1)))
pygame.display.update()
""""Execution loop"""
while 1:
vetor = []
temp = []
ready = 0
repeat = 0
windowSurface.fill(collor[0])
pygame.display.update()
while ready == 0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_0:
temp.append('0')
elif event.key == pygame.K_1:
temp.append('1')
elif event.key == pygame.K_2:
temp.append('2')
elif event.key == pygame.K_3:
temp.append('3')
elif event.key == pygame.K_4:
temp.append('4')
elif event.key == pygame.K_5:
temp.append('5')
elif event.key == pygame.K_6:
temp.append('6')
elif event.key == pygame.K_7:
temp.append('7')
elif event.key == pygame.K_8:
temp.append('8')
elif event.key == pygame.K_9:
temp.append('9')
elif event.key == pygame.K_SPACE:
vetor.append(int("".join(temp)))
temp = []
elif event.key == pygame.K_RETURN:
ready = 1
elif event.key == pygame.K_r:
vetor = range(200)
random.shuffle(vetor)
ready = 1
pygame.display.set_caption("Sorting...")
update()
for i in xrange(len(vetor)):
m = vetor[i]
pos = i
for j in xrange(i + 1, len(vetor)):
if vetor[j] < m:
m = vetor[j]
pos = j
vetor[pos] = vetor[i]
vetor[i] = m
update()
pygame.display.set_caption("Done!")
while repeat == 0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
repeat = 1