-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionsort.py
More file actions
119 lines (101 loc) · 3.34 KB
/
Copy pathInsertionsort.py
File metadata and controls
119 lines (101 loc) · 3.34 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
#random values being generated are in negatives. SO even though it sorts in ascending order, the first graph looks tallest.
import random
import pygame
import sys
import time
bars=[]
pygame.init()
num_bar=20 #no. of bars
bar_width=15 #width of bars
space=5 #space between bars
sorting=False
font = pygame.font.SysFont('Times New Roman', 30)
blue = (0, 0, 255)
black=(0,0,0)
screen=pygame.display.set_mode((800,600))
screen.fill((black))
#To make the graph
def drawbar(x, height,color):
pygame.draw.rect(screen, color, (x,400,bar_width, height), 0)
def button(msg,x,y,w,h):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
global sorting
if x+w > mouse[0] > x and y+h > mouse[1] > y: #if mouse pointer is over button
pygame.draw.rect(screen, (225,225,225),(x,y,w,h), 0)
if click[0] == 1: #to break the continuos while loop
sorting = True
else:
pygame.draw.rect(screen, (200,127,168),(x,y,w,h), 0)
text = font.render(msg, True, (0, 0, 0)) #text on button
screen.blit(text, (x + 10, y + 10))
#Exit Button
def button2(msg,x,y,w,h):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
global sorting
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, (225,225,225),(x,y,w,h), 0)
if click[0] == 1:
pygame.quit()
else:
pygame.draw.rect(screen, (200,127,168),(x,y,w,h), 0)
text = font.render(msg, True, (0, 0, 0))
screen.blit(text, (x + 10, y + 10))
def color(i,j,k):
x=(k*bar_width)+(k*space)+(600-(num_bar *bar_width+num_bar*space))
height=bars[k]
if bars[k] is bars[j]:
color = (255,127,80)
elif k is i:
return
else:
color=blue
drawbar(x,height,color)
def insertionsort(arr):
n=len(arr)
for i in range(1,n):
key=arr[i]
j=i-1
for k in range(num_bar):
color(i,j,k)
xi=(i*bar_width)+(i*space)+(600-(num_bar *bar_width+num_bar*space))
drawbar(xi,arr[i],(200,200,200))
pygame.display.update()
time.sleep(.6)
while(j>=0 and key<arr[j]):
arr[j+1]=arr[j]
j-=1
for k in range(num_bar):
color(i,j,k)
xi=(i*bar_width)+(i*space)+(600-(num_bar *bar_width+num_bar*space))
drawbar(xi,arr[i],(200,200,200))
pygame.display.update()
time.sleep(.5)
arr[j+1]=key
screen.fill(black)
for k in range(num_bar):
xi=(k*bar_width)+(k*space)+(600-(num_bar *bar_width+num_bar*space))
drawbar(xi,arr[k],(200,200,200))
print(arr[k])
pygame.display.update()
#Creating all values
for i in range(num_bar):
height=random.randint(-100,-10)
x=(i*bar_width)+(i*space)+(600 -(num_bar *bar_width+num_bar*space))
drawbar(x,height,blue)
bars.append(height)
#To keep working till user clicks Insertion Button or Exits
while True:
button('Insertion', 300, 100, 130, 50)
button2("Exit",500,100,70,50)
pygame.display.update()
if sorting:
break
for event in pygame.event.get():
if(event==pygame.QUIT):
pygame.quit()
sys.exit()
insertionsort(bars)
print("SORTED")
time.sleep(1)