Skip to content

Commit 5e0a514

Browse files
committed
completed bubble sort visualization
1 parent f81cd7f commit 5e0a514

File tree

5 files changed

+59
-22
lines changed

5 files changed

+59
-22
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
# Sorting-Algorithm-Visualizer
1+
# Algorithm-Visualizer
22

3-
### Sorting Algorithms Covered:
3+
### Algorithms Covered:
4+
- Searching
5+
- Linear Search
6+
- Binary Search
7+
- Ternery Search
8+
- Sorting
49
- Bubble Sort
510
- Selection Sort
611
- Insertion Sort

algorithms/bubble_sort.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from time import sleep
2+
colorData=[]
3+
4+
def startBubbleSort(data, drawData,stepTime):
5+
6+
colorData=['grey' for x in range(len(data))]
7+
for i in range(len(data)-1):
8+
for j in range(len(data)-i-1):
9+
colorData[j]=colorData[j+1]='green'
10+
drawData(data,colorData)
11+
colorData[j]=colorData[j+1]='grey'
12+
sleep(stepTime)
13+
if data[j]>data[j+1]:
14+
data[j],data[j+1]=data[j+1],data[j]
15+
colorData[j]=colorData[j+1]='red'
16+
drawData(data,colorData)
17+
colorData[j]=colorData[j+1]='grey'
18+
sleep(stepTime)
19+
colorData[len(data)-1-i]='white'
20+
drawData(data,colorData)
21+
colorData[0]='white'
22+
drawData(data,colorData)

bubble_sort.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

gui.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
from tkinter import *
22
from tkinter import ttk
3-
from bubble_sort import startBubbleSort
43
import random
4+
sys.path.insert(1,'C:\\Users\\skili\\Documents\\GitHub\\Sorting-Algorithm-Visualizer\\algorithms')
5+
#importing our specially designed algorithms
6+
# from linear_search import startLinearSearch
7+
# from binary_search import startBinarySearch
8+
from bubble_sort import startBubbleSort
9+
# from insertion_sort import startInsertionSort
10+
# from selection_sort import startSelectionSort
11+
# from merge_sort import startMergeSort
12+
# from quick_sort import startQuickSort
513

14+
#to store the array/heights of rectangle
615
data = []
16+
#to store the colour of respective rectangles
17+
colorData = []
18+
719

8-
def visualise(algorithm,speed):
20+
21+
#to start visualization
22+
def visualize(algorithm,stepTime):
923
if algorithm=="Bubble sort":
10-
startBubbleSort(data,drawData)
24+
startBubbleSort(data,drawData,stepTime)
1125

1226

1327

@@ -31,17 +45,17 @@ def move_window(event):
3145

3246
#function to generate random data
3347
def genData(data_size):
34-
35-
global data
36-
data=[]
48+
global data,colorData
49+
data=colorData=[]
50+
colorData=['grey' for x in range(int(float(data_size)))]
3751
for _ in range(int(float(data_size))):
3852
data.append(random.randrange(1,100))
39-
drawData(data)
53+
drawData(data,colorData)
4054

4155

4256

4357
#function to draw rectangles, 'data' is a list of rectangle heights
44-
def drawData(data):
58+
def drawData(data,colorData):
4559
#clears canvas before drawing new data
4660
canvas.delete("all")
4761
#setting canvas height,width
@@ -62,7 +76,7 @@ def drawData(data):
6276
y0=canvas_h-height*(canvas_h-20)
6377
x1=(i+1)*rectangle_w+(i+1)*spacing
6478
y1=canvas_h
65-
canvas.create_rectangle(x0,y0,x1,y1,fill="grey")
79+
canvas.create_rectangle(x0,y0,x1,y1,fill=colorData[i])
6680
root.update_idletasks()
6781

6882

@@ -90,7 +104,7 @@ def main():
90104
algorithm_menu=ttk.Combobox(topf,textvariable=algorithm,values=['Bubble sort','Selection Sort'])
91105
algorithm_menu.grid(row=0,column=1,padx=5,pady=5)
92106
algorithm_menu.current(0)
93-
Button(topf,text="Visualise",bg='white',command=lambda : visualise(algorithm.get(),speed.get())).grid(row=0,column=2,padx=5,pady=5)
107+
Button(topf,text="Visualise",bg='white',command=lambda : visualize(algorithm.get(),speed.get())).grid(row=0,column=2,padx=5,pady=5)
94108

95109
#row 2 of topf
96110
global size,speed
@@ -108,7 +122,7 @@ def main():
108122

109123
#canvas for visualisation
110124
global canvas
111-
canvas =Canvas(root,width=600,height=380,bg='black')
125+
canvas = Canvas(root,width=600,height=380,bg='black')
112126
canvas.grid(row=1,column=0,padx=10,pady=5)
113127
# data=genData(50)
114128
# drawData(data)

tempCodeRunnerFile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data=[10,20,30,40,50]
2+
#normalizing the data
3+
for i in len(data):
4+
data[i]=data[i]/max(data)
5+
print(data)

0 commit comments

Comments
 (0)