Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6dd6d68

Browse files
committed
app layout updated and bubble sort added
1 parent 12fe227 commit 6dd6d68

File tree

9 files changed

+159
-3
lines changed

9 files changed

+159
-3
lines changed

‎.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/Sorting Algorithms Visualization Python.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.88 KB
Binary file not shown.

‎main.py

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,107 @@
1+
# needed libraries
12
from tkinter import *
3+
import tkinter.ttk
4+
from sorting_algorithms import *
5+
import random
26

37
root = Tk()
4-
root.title('Sorting Algorithms Visualization Python')
8+
root.title('Sorting Algorithms Visualization')
59
root.geometry('900x600')
6-
root.config(bg='#fff')
10+
root.config(background='#fff')
711

8-
root.mainloop()
12+
# variables
13+
dataset = []
14+
stop_flag = False
15+
sorting_algorithm = StringVar()
16+
17+
18+
# Buttons
19+
def start_btn():
20+
global dataset, stop_flag
21+
if stop_flag:
22+
stop_flag = False
23+
if sorting_algorithm.get() == 'Bubble Sort':
24+
SortingAlgorithms.bubble_sort(dataset, draw_data, animation_speed.get(), stop_flag)
25+
# this part will be updated
26+
27+
28+
def stop_btn():
29+
global stop_flag
30+
stop_flag = True
31+
32+
33+
def reset_btn():
34+
global dataset
35+
dataset = []
36+
draw_data(dataset, [])
37+
38+
39+
# Data manipulation
40+
def draw_data(data_set, clr):
41+
cv.delete('all')
42+
cv_height = 380
43+
cv_width = 700
44+
x_width = cv_width / (len(data_set) + 1)
45+
offset = 30
46+
space_between = 5
47+
data = [i / max(data_set) for i in data_set]
48+
49+
for i, h in enumerate(data):
50+
x0 = i * x_width + offset + space_between
51+
y0 = cv_height - h * 320
52+
x1 = (i + 1) * x_width + offset
53+
y1 = cv_height
54+
55+
cv.create_rectangle(x0, y0, x1, y1, fill=clr[i])
56+
cv.create_text(x0 + 2, y0, anchor=SW, text=str(data_set[i]))
57+
58+
root.update_idletasks()
59+
60+
61+
def generate_dataset():
62+
global dataset
63+
min_val = int(dataset_min_value.get())
64+
max_val = int(dataset_max_value.get())
65+
data_size = int(dataset_size.get())
966

67+
dataset = []
68+
for i in range(data_size):
69+
dataset.append(random.randrange(min_val, max_val + 1))
70+
# draw data
71+
draw_data(dataset, ['#FF597B' for i in range(len(dataset))])
72+
73+
74+
# GUI Setup
75+
sidebar_fr = Frame(root, width=220, height=230, background='#ECF2FF')
76+
sidebar_fr.grid(row=0, column=0, rowspan=3, sticky='ns')
77+
78+
header = Frame(root, width=820, height=130, background='#F3F1F5', padx=0, pady=0)
79+
header.grid(row=0, column=1, padx=0, pady=5, columnspan=1)
80+
81+
cv = Canvas(root, width=820, height=480, background='#fff')
82+
cv.grid(row=1, column=1, padx=0, pady=5, columnspan=1)
83+
84+
combobox = tkinter.ttk.Combobox(sidebar_fr, values=['Bubble Sort', 'Quick Sort'], textvariable=sorting_algorithm)
85+
combobox.grid(row=1, column=0, padx=5, pady=5)
86+
combobox.current(0)
87+
88+
89+
dataset_size = Scale(sidebar_fr, from_=3, to=50, resolution=1, orient=HORIZONTAL, label="Dataset Size", background='#fff', length=150)
90+
dataset_size.grid(row=2, column=0, padx=5, pady=5, sticky=W)
91+
92+
dataset_min_value = Scale(sidebar_fr, from_=1, to=100, resolution=1, orient=HORIZONTAL, label="Minimum Value", background='#fff', length=150)
93+
dataset_min_value.grid(row=3, column=0, padx=5, pady=5, sticky=W)
94+
95+
dataset_max_value = Scale(sidebar_fr, from_=100, to=1000, resolution=1, orient=HORIZONTAL, label="Maximum Value", background='#fff', length=150)
96+
dataset_max_value.grid(row=4, column=0, padx=5, pady=5, sticky=W)
97+
98+
animation_speed = Scale(sidebar_fr, from_=0.1, to=5.0, length=150, digits=2, resolution=0.1, orient=HORIZONTAL, label='Select Speed(sec)', background='#fff')
99+
animation_speed.grid(row=5, column=0, padx=5, pady=5, sticky=W)
100+
101+
Button(sidebar_fr, text='Generate Dataset', command=generate_dataset, bg='#764AF1', fg='white', width=20).grid(row=6, column=0, padx=5, pady=5)
102+
Button(sidebar_fr, text='Start', command=start_btn, bg='#019267', fg='white', height=1, width=20).grid(row=7, column=0, padx=5, pady=5)
103+
Button(sidebar_fr, text='Reset', command=reset_btn, bg='#FF597B', fg='white', height=1, width=20).grid(row=8, column=0, padx=5, pady=5)
104+
Button(sidebar_fr, text='Stop', command=stop_btn, bg='orange', fg='white', height=1, width=20).grid(row=9, column=0, padx=5, pady=5)
105+
106+
# start the main event loop of the Application
107+
root.mainloop()

‎sorting_algorithms.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import time
2+
3+
4+
class SortingAlgorithms:
5+
6+
# Bubble Sort
7+
@staticmethod
8+
def bubble_sort(data_set, draw_d, speed, stop_flag):
9+
for i in range(len(data_set) - 1):
10+
for j in range(len(data_set) - 1):
11+
if stop_flag:
12+
return
13+
if data_set[j] > data_set[j + 1]:
14+
# swap datas
15+
data_set[j], data_set[j + 1] = data_set[j + 1], data_set[j]
16+
draw_d(data_set, ['#019267' if c == j or c == j + 1 else 'red' for c in range(len(data_set))])
17+
time.sleep(speed)
18+
19+
draw_d(data_set, ['#019267' for i in range(len(data_set))])
20+
21+
# this part will be updated...

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /