-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_algorithms.py
160 lines (152 loc) · 5.75 KB
/
sort_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
import pygame
import time
from cell import allow_quit
def v_bubble_sort(lst, w, screen, delay):
delay = delay/100
comparisons = 0
swaps = 0
font = pygame.font.Font('freesansbold.ttf', 15)
for i in range(len(lst)):
for j in range(len(lst)-i-1):
comparisons += 1
if lst[j] > lst[j+1]:
time.sleep(delay)
swaps += 1
lst[j], lst[j+1] = lst[j+1], lst[j]
screen.fill((0, 0, 0))
x = 0
text = font.render(f'comparisons: {comparisons}, swaps: {swaps}', True, (255, 255, 255), (0, 0, 0))
for v in range(len(lst)):
color = (255, 255, 255)
if v == j+1:
color = (255, 0, 0)
elif v > len(lst)-i-1:
color = (0, 255, 0)
pygame.draw.rect(screen,
color,
(x, 600, w, -lst[v]))
screen.blit(text, (1, 1))
x += w+((1/4)*w*2)
pygame.display.flip()
allow_quit()
return swaps, comparisons
class count_quick:
def __init__(self):
self.swaps = 0
self.comparisons = 0
self.pivots = []
def v_quick_sort(lst, w, screen, delay, left=0, right=None, first_rec=True):
global count
font = pygame.font.Font('freesansbold.ttf', 15)
if first_rec:
count = count_quick()
if type(right) != int:
right = len(lst)-1
if left >= right:
screen.fill((0, 0, 0))
x = 0
count.pivots = count.pivots + [left, right]
text = font.render(f'comparisons: {count.comparisons}, swaps: {count.swaps}', True, (255, 255, 255), (0, 0, 0))
for v in range(len(lst)):
color = (255, 255, 255)
if v in count.pivots:
color = (0, 255, 0)
screen.blit(text, (1, 1))
pygame.draw.rect(screen, color, (x, 600, w, -lst[v]))
x += w+((1/4)*w*2)
time.sleep(delay/100)
pygame.display.flip()
return
i = left
j = right - 1
pivot = lst[right]
while i < j:
while i < right and lst[i] < pivot:
count.comparisons += 1
i = i + 1
while j > left and lst[j] >= pivot:
count.comparisons += 1
j = j - 1
if i < j:
count.comparisons += 1
tmp = lst[i]
lst[i] = lst[j]
lst[j] = tmp
count.swaps += 1
screen.fill((0, 0, 0))
x = 0
text = font.render(f'comparisons: {count.comparisons}, swaps: {count.swaps}', True, (255, 255, 255),
(0, 0, 0))
for v in range(len(lst)):
color = (255, 255, 255)
if v == j:
color = (255, 0, 0)
elif v == i:
color = (0, 0, 255)
elif v in count.pivots:
color = (0, 255, 0)
pygame.draw.rect(screen, color, (x, 600, w, -lst[v]))
screen.blit(text, (1, 1))
x += w+((1/4)*w*2)
allow_quit()
time.sleep(delay/100)
pygame.display.flip()
else:
count.pivots.append(i)
if lst[i] > pivot:
count.comparisons += 1
count.swaps += 1
tmp = lst[i]
lst[i] = lst[right]
lst[right] = tmp
v_quick_sort(lst, w, screen, delay, left, i-1, False)
v_quick_sort(lst, w, screen, delay, i+1, right, False)
return count.swaps, count.comparisons
def v_insertion_sort(lst, w, screen, delay):
delay = delay/100
comparisons = 0
swaps = 0
font = pygame.font.Font('freesansbold.ttf', 16)
for i in range(len(lst)):
for j in range(i, -1, -1):
x = 0
screen.fill((0, 0, 0))
if j != 0:
comparisons += 1
if lst[j-1] < lst[i] < lst[j]:
swaps += 1
lst.insert(j, lst.pop(i))
time.sleep(delay)
text = font.render(f'comparisons: {comparisons}, swaps: {swaps}', True, (255, 255, 255), (0, 0, 0))
for v in range(len(lst)):
color = (255, 255, 255)
if v == i+1:
color = (255, 0, 0)
if v == j:
color = (0, 255, 0)
pygame.draw.rect(screen,
color,
(x, 600, w, -lst[v]))
x += w+((1/4)*w*2)
screen.blit(text, (1, 1))
pygame.display.flip()
allow_quit()
break
else:
comparisons += 1
if lst[i] < lst[j]:
swaps += 1
lst.insert(j, lst.pop(i))
time.sleep(delay)
text = font.render(f'comparisons: {comparisons}, swaps: {swaps}', True, (255, 255, 255), (0, 0, 0))
for v in range(len(lst)):
color = (255, 255, 255)
pygame.draw.rect(screen,
color,
(x, 600, w, -lst[v]))
x += w+((1/4)*w*2)
screen.blit(text, (1, 1))
pygame.display.flip()
allow_quit()
break
return swaps, comparisons