Skip to content

Commit a835b05

Browse files
committed
Added bubble sort simulation
1 parent d7aed7e commit a835b05

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

Diff for: Bubble-Sort-Visualization/bubble_sort.py

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
from tkinter import *
2+
from tkinter import messagebox
3+
import random
4+
import time
5+
6+
unsorted_data = data = []
7+
8+
def draw_data(data, colors):
9+
""""
10+
Function that is used to draw the array of data
11+
12+
Inputs:
13+
data => array of the data to be drawn
14+
colors => array carrying the corresponding color of each value in the array
15+
16+
"""
17+
canvas.delete("all")
18+
canvas_height = 500
19+
canvas_width = 800
20+
x_width = canvas_width / (len(data) + 1)
21+
offset = 20
22+
spacing = 10
23+
24+
# Normalizing the heights of the bars
25+
normalize_data = [i / max(data) for i in data]
26+
27+
for i, rec_height in enumerate(normalize_data):
28+
x_initial = i * x_width + offset + spacing
29+
y_initial = canvas_height - rec_height * 460
30+
31+
x_final = (i+1) * x_width + offset
32+
y_final = canvas_height
33+
34+
canvas.create_rectangle(x_initial, y_initial, x_final, y_final, fill=colors[i])
35+
canvas.create_text(x_initial + 2, y_initial, anchor=SW, text=str(data[i]))
36+
37+
# Updating the canvas after sleep
38+
main_prog.update_idletasks();
39+
40+
41+
def generate_array():
42+
""""
43+
Function that is used to generate an array of data on clicking on the generate button in the UI_frame
44+
45+
"""
46+
global data, unsorted_data
47+
48+
# Reading user inputs
49+
min_val = int(min_value.get())
50+
size_val = int(size_value.get())
51+
max_val = int(max_value.get())
52+
53+
if(min_val > max_val):
54+
messagebox.showwarning(message="Max. value should not be less than Min. value")
55+
min_val, max_val = max_val, min_val
56+
57+
data = []
58+
59+
for i in range(size_val):
60+
data.append(random.randrange(min_val, max_val + 1))
61+
62+
unsorted_data = data.copy()
63+
draw_data(data, [ "red" for x in range(len(data)) ])
64+
65+
66+
67+
def reset_array():
68+
"""
69+
Function that resets the current sorted array
70+
71+
"""
72+
global data
73+
data = unsorted_data.copy();
74+
draw_data(data, [ "red" for x in range(len(data)) ])
75+
76+
77+
78+
def bubble_sort(data, speed):
79+
"""
80+
Function that performs bubble sort on the passed array
81+
82+
Inputs:
83+
data => array to be sorted
84+
speed => speed of the simulation
85+
"""
86+
87+
for i in range(len(data)-1):
88+
for j in range(len(data)- i - 1):
89+
if data[j] > data[j+1]:
90+
data[j], data[j+1] = data[j+1], data[j]
91+
draw_data(data, ["yellow" if x == j or x == j+1 else "red" for x in range(len(data))])
92+
time.sleep(speed)
93+
94+
draw_data(data, ["green" for x in range(len(data))])
95+
96+
97+
def run_sort():
98+
"""
99+
Function that runs the simulation on user click
100+
101+
"""
102+
global data
103+
bubble_sort(data, speed_scale.get())
104+
105+
## Intialzing main program
106+
main_prog = Tk()
107+
main_prog.title("Bubble Sort Visualization")
108+
main_prog.maxsize(800, 800)
109+
main_prog.config(bg="grey")
110+
111+
112+
## Creating user interface frame
113+
UI_frame = Frame(main_prog, width=800, height=300, bg="grey")
114+
UI_frame.grid(row=0, column=0)
115+
Label(UI_frame, text="Let's learn Bubble Sort", bg="grey").grid(row=0, column=0, padx=5, pady=5, sticky=W)
116+
Button(UI_frame, text="Start", command=run_sort, bg="green").grid(row=0, column=1, padx=5, pady=5)
117+
118+
speed_scale = Scale(UI_frame, from_=0.1, to=2.0, length=200, digits=2, resolution=0.2, orient=HORIZONTAL, label="Select Speed", bg="ivory")
119+
speed_scale.grid(row=0, column=2, padx=5, pady=5)
120+
121+
## Creating scale for array size
122+
size_value = Scale(UI_frame, from_=0, to=30, resolution=1, orient=HORIZONTAL, label="Select Size", bg="ivory")
123+
size_value.grid(row=1, column=0, padx=5, pady=5, sticky=W)
124+
125+
## Creating scale for min. value of generated values
126+
min_value = Scale(UI_frame, from_=0, to=100, resolution=10, orient=HORIZONTAL, label="Select Min. value", bg="ivory")
127+
min_value.grid(row=1, column=1, padx=5, pady=5, sticky=W)
128+
129+
## Creating scale for max. value of generated values
130+
max_value = Scale(UI_frame, from_=0, to=500, resolution=10, orient=HORIZONTAL, label="Select Max. value", bg="ivory")
131+
max_value.grid(row=1, column=2, padx=5, pady=5, sticky=W)
132+
133+
# Generate random array button
134+
Button(UI_frame, text="Generate",command=generate_array, bg="blue").grid(row=2, column=2, padx=5, pady=5)
135+
136+
# Reset the current array button
137+
Button(UI_frame, text="Reset",command=reset_array, bg="blue").grid(row=2, column=1, padx=5, pady=5)
138+
139+
140+
## Creating canvas for visualization
141+
canvas = Canvas(main_prog, width=800, height=500, bg="white")
142+
canvas.grid(row=1, column=0)
143+
144+
145+
146+
## Running main program
147+
main_prog.mainloop()
148+

0 commit comments

Comments
 (0)