Skip to content

Commit 8736b04

Browse files
authored
Merge pull request #697 from RaveenaBhasin/quick_sort
Quick sort algorithm visualizer added
2 parents e754e2e + 3210d84 commit 8736b04

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# QUICK SORT VISUALIZER
2+
3+
## Introduction
4+
This script helps you see the quick sort algorithm in action, so that it can be easily understood.
5+
6+
## Modules used
7+
- Tkinter
8+
- Random
9+
- Time
10+
- Sys
11+
12+
## Installing the modules
13+
14+
``
15+
pip install tkinter
16+
``
17+
18+
Rest of the modules are already present.
19+
20+
## How to use?
21+
- Enter size of the array.
22+
- Enter the range i.e. minimum and maximum value of elements.
23+
- Click on **Generate** button to create a random array. Every click will generate a random array.
24+
- Click on **Sort** button to perform quick sort.
25+
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
# quick sort visualizer
2+
3+
# import tkinter as tk
4+
from tkinter import Tk, Label, Button, Frame, Canvas, Entry, SW, W
5+
from tkinter import messagebox
6+
import random
7+
import time
8+
import sys
9+
10+
sys.setrecursionlimit(10**6)
11+
12+
# colours
13+
DARK_GREY = '#73C6B6'
14+
LIGHT_GREY = '#B2BABB'
15+
WHITE = '#F0F3F4'
16+
GREEN = '#82E0AA'
17+
GREEN_2 = '#76D7C4'
18+
BLUE = '#85C1E9'
19+
PURPLE = '#BB8FCE'
20+
RED = '#F5B7B1'
21+
YELLOW = '#F7E806'
22+
23+
# array of elements / rectangle heights
24+
array = []
25+
26+
27+
# ~30 elements fit in the canvas using below function
28+
def drawRect(array, color):
29+
canvas.delete("all")
30+
c_height = 380
31+
c_width = 1000
32+
x_width = c_width / (len(array) + 1)
33+
x_left = 15
34+
spacing = 10
35+
normalizedArray = [i / max(array) for i in array]
36+
for i, height in enumerate(normalizedArray):
37+
# top left
38+
x0 = i * x_width + x_left + spacing
39+
y0 = c_height - height * 340
40+
# bottom right
41+
x1 = (i + 1) * x_width + x_left
42+
y1 = c_height
43+
canvas.create_rectangle(x0, y0, x1, y1, fill=color[i])
44+
canvas.create_text(x0 + 2, y0, anchor=SW, text=str(array[i]))
45+
46+
root.update_idletasks()
47+
48+
49+
# generate random elements for the array and
50+
# draw their rectangles on the canvas
51+
def Generate():
52+
global array
53+
try:
54+
minVal = int(minEntry.get())
55+
maxVal = int(maxEntry.get())
56+
size = int(sizeEntry.get())
57+
except Exception:
58+
messagebox.showwarning("Message", "Enter all values correctly")
59+
60+
array = []
61+
# generating random list
62+
color = []
63+
for _ in range(size):
64+
array.append(random.randrange(minVal, maxVal + 1))
65+
color.append(GREEN_2)
66+
67+
drawRect(array, color)
68+
69+
70+
# partition function
71+
def partition(array, left, right, drawRect):
72+
i = left + 1
73+
pivot = array[left]
74+
75+
for j in range(left + 1, right + 1):
76+
if array[j] < pivot:
77+
array[i], array[j] = array[j], array[i]
78+
i += 1
79+
array[left], array[i - 1] = array[i - 1], array[left]
80+
return i - 1
81+
82+
83+
# quick sort function
84+
def quickSort(array, left, right, drawRect):
85+
if left < right:
86+
pivot = partition(array, left, right, drawRect)
87+
quickSort(array, left, pivot, drawRect)
88+
quickSort(array, pivot + 1, right, drawRect)
89+
drawRect(array, [BLUE if x >= left and x < pivot
90+
else YELLOW if x == pivot
91+
else PURPLE if x > pivot and x <= right
92+
else RED for x in range(len(array))])
93+
94+
time.sleep(0.5)
95+
drawRect(array, [GREEN for x in range(len(array))])
96+
97+
98+
# actually perform quicksort
99+
def sort():
100+
try:
101+
quickSort(array, 0, len(array) - 1, drawRect)
102+
messagebox.showinfo('Succces', 'Array sorted!')
103+
except Exception:
104+
messagebox.showinfo('Error', 'Array could not be sorted')
105+
106+
107+
# !--GUI code starts--!
108+
109+
# main window
110+
root = Tk()
111+
root.title('Quick Sort Visualizer')
112+
# background color
113+
root.config(bg=LIGHT_GREY)
114+
# disabling resizing of window
115+
root.resizable(0, 0)
116+
117+
# ---adding frames---
118+
# top name frame
119+
top = Frame(root,
120+
width=1300,
121+
height=200,
122+
bg=GREEN_2,
123+
bd=8,
124+
relief="groove")
125+
top.grid(row=0, column=0, padx=10, pady=5)
126+
127+
# frame for canvas
128+
canvas = Canvas(root,
129+
width=1000,
130+
height=380,
131+
bg=WHITE)
132+
canvas.grid(row=1, column=0, padx=10, pady=5)
133+
134+
# frame for user entries
135+
entries = Frame(root,
136+
width=1300,
137+
height=300,
138+
bg=GREEN_2,
139+
bd=8,
140+
relief="groove")
141+
entries.grid(row=2, column=0, padx=10, pady=5)
142+
143+
144+
# ---adding widgets---
145+
# top label
146+
greeting = Label(top,
147+
text="Quick Sort Visualizer",
148+
width=62,
149+
font=("Courier New", 20, "bold"),
150+
background=GREEN_2)
151+
greeting.grid(row=0, column=1, pady=5)
152+
153+
154+
# user entries and buttons
155+
# row 0
156+
Size = Label(entries,
157+
text="Size of array : ",
158+
bg=LIGHT_GREY,
159+
relief="groove")
160+
Size.grid(row=0,
161+
column=0,
162+
padx=15,
163+
pady=5,
164+
sticky=W,
165+
ipadx=20,
166+
ipady=5)
167+
sizeEntry = Entry(entries, justify="center")
168+
sizeEntry.grid(row=0, column=1, padx=15, pady=5, sticky=W, ipady=5)
169+
170+
minn = Label(entries,
171+
text="Minimum element : ",
172+
bg=LIGHT_GREY,
173+
relief="groove")
174+
minn.grid(row=0, column=2, padx=15, pady=5, sticky=W, ipadx=20, ipady=5)
175+
minEntry = Entry(entries, justify="center")
176+
minEntry.grid(row=0, column=3, padx=15, pady=5, sticky=W, ipady=5)
177+
178+
maxx = Label(entries,
179+
text="Maximum element : ",
180+
bg=LIGHT_GREY,
181+
relief="groove")
182+
maxx.grid(row=0, column=4, padx=15, pady=5, sticky=W, ipadx=20, ipady=5)
183+
maxEntry = Entry(entries, justify="center")
184+
maxEntry.grid(row=0, column=5, padx=15, pady=5, sticky=W, ipady=5)
185+
186+
# row 1
187+
generate = Button(entries, text="Generate", bg=LIGHT_GREY, command=Generate)
188+
generate.grid(row=1, column=2, padx=15, pady=5, ipadx=20, ipady=5)
189+
190+
Search = Button(entries, text="Sort", bg=LIGHT_GREY, command=sort)
191+
Search.grid(row=1, column=3, padx=15, pady=5, ipadx=20, ipady=5)
192+
193+
root.mainloop()
194+
195+
# !--GUI code ends--!

0 commit comments

Comments
 (0)