1+ # Imports
2+ import pygame
3+ import random
4+ pygame .font .init ()
5+
6+ # Total window
7+ screen = pygame .display .set_mode (
8+ (900 , 650 )
9+ )
10+
11+ # Title and Icon
12+ pygame .display .set_caption ("SORTING VISUALISER" )
13+
14+ # Boolean variable to run
15+ # the program in while loop
16+ run = True
17+
18+ # Window size and some initials
19+ width = 900
20+ length = 600
21+ array = [0 ]* 151
22+ arr_clr = [(0 , 204 , 102 )]* 151
23+ clr_ind = 0
24+ clr = [(0 , 204 , 102 ), (255 , 0 , 0 ),
25+ (31 , 133 , 153 ), (157 , 102 , 255 )]
26+ fnt = pygame .font .SysFont ("comicsans" , 30 )
27+ fnt1 = pygame .font .SysFont ("comicsans" , 20 )
28+
29+
30+ # Function to generate new Array
31+ def generate_arr ():
32+ for i in range (1 , 151 ):
33+ arr_clr [i ]= clr [0 ]
34+ array [i ]= random .randrange (1 , 100 )
35+
36+ # Intially generate a array
37+ generate_arr ()
38+
39+ # Function to refill the
40+ # updates on the window
41+ def refill ():
42+ screen .fill ((255 , 255 , 255 ))
43+ draw ()
44+ pygame .display .update ()
45+ pygame .time .delay (30 )
46+
47+ # Sorting Algo:Quick sort
48+ def quicksort (array , l , r ):
49+ if l < r :
50+ pi = partition (array , l , r )
51+ quicksort (array , l , pi - 1 )
52+ refill ()
53+ for i in range (0 , pi + 1 ):
54+ arr_clr [i ]= clr [3 ]
55+ quicksort (array , pi + 1 , r )
56+
57+ # Function to partition the array
58+ def partition (array , low , high ):
59+ pygame .event .pump ()
60+ pivot = array [high ]
61+ arr_clr [high ]= clr [2 ]
62+ i = low - 1
63+ for j in range (low , high ):
64+ arr_clr [j ]= clr [1 ]
65+ refill ()
66+ arr_clr [high ]= clr [2 ]
67+ arr_clr [j ]= clr [0 ]
68+ arr_clr [i ]= clr [0 ]
69+ if array [j ]< pivot :
70+ i = i + 1
71+ arr_clr [i ]= clr [1 ]
72+ array [i ], array [j ]= array [j ], array [i ]
73+ refill ()
74+ arr_clr [i ]= clr [0 ]
75+ arr_clr [high ]= clr [0 ]
76+ array [i + 1 ], array [high ] = array [high ], array [i + 1 ]
77+
78+ return ( i + 1 )
79+
80+ # Function to Draw the
81+ # array values
82+ def draw ():
83+ # Text should be rendered
84+ txt = fnt .render ("SORT : PRESS 'ENTER'" ,
85+ 1 , (0 , 0 , 0 ))
86+
87+ # Position where text is placed
88+ screen .blit (txt , (20 , 20 ))
89+ txt1 = fnt .render ("NEW ARRAY : PRESS 'R'" ,
90+ 1 , (0 , 0 , 0 ))
91+ screen .blit (txt1 , (20 , 40 ))
92+ txt2 = fnt1 .render ("ALGORITHM USED: QUICK SORT" ,
93+ 1 , (0 , 0 , 0 ))
94+ screen .blit (txt2 , (600 , 60 ))
95+ element_width = (width - 150 )// 150
96+ boundry_arr = 900 / 150
97+ boundry_grp = 550 / 100
98+ pygame .draw .line (screen , (0 , 0 , 0 ),
99+ (0 , 95 ), (900 , 95 ), 6 )
100+
101+ # Drawing the array values as lines
102+ for i in range (1 , 151 ):
103+ pygame .draw .line (screen ,
104+ arr_clr [i ], (boundry_arr * i - 3 , 100 ),
105+ (boundry_arr * i - 3 ,
106+ array [i ]* boundry_grp + 100 ),
107+ element_width )
108+
109+ # Program should be run
110+ # continuously to keep the window open
111+ while run :
112+ # background
113+ screen .fill ((255 , 255 , 255 ))
114+
115+ # Event handler stores all event
116+ for event in pygame .event .get ():
117+
118+ # If we click Close button in window
119+ if event .type == pygame .QUIT :
120+ run = False
121+ if event .type == pygame .KEYDOWN :
122+ if event .key == pygame .K_r :
123+ generate_arr ()
124+ if event .key == pygame .K_RETURN :
125+ quicksort (array , 1 , len (array )- 1 )
126+ draw ()
127+ pygame .display .update ()
128+
129+ pygame .quit ()
0 commit comments