-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortingVisualiser.py
More file actions
114 lines (92 loc) · 3.47 KB
/
Copy pathsortingVisualiser.py
File metadata and controls
114 lines (92 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Sorting Algorithms Visualiser
# Andy Lin 28/04/2020
from insertionSort import insertion_sort
from mergeSort import merge_sort
from bubbleSort import bubble_sort
from quickSort import quick_sort
from heapSort import heap_sort
from tkinter import *
from tkinter import ttk
import random
# variables
algorithms = ['Mergesort', 'Quicksort', 'Bubblesort', 'Insertionsort', 'Heapsort']
MAXWIDTH = 900
MAXHEIGHT = 700
MAXNUM = 400
MINNUM = 1
array = []
# starting window
window = Tk()
window.maxsize(MAXWIDTH, MAXHEIGHT)
window.title("Algorithms Visualiser")
window.config(bg="black")
selected_alg = StringVar()
# functions
def drawArray(data, colourArray):
canvas.delete("all")
c_height = 480
c_width = MAXWIDTH
x_width = c_width / len(data)
normalisedData = [i/max(data) for i in data]
for i, height in enumerate(normalisedData):
# top left hand corner of bar
x0= i*x_width
y0= c_height - height*(c_height-100)
# bottom right hand corner of bar
x1 = (i+1)*x_width
y1 = c_height
# draw the rectangles
canvas.create_rectangle(x0, y0, x1, y1, fill = colourArray[i])
window.update_idletasks()
def generate():
""" Generates a new array based on user inputs """
global array
sizeNum = sizeEntry.get()
# create array of random integers from MINNUM to MAXNUM
array = []
for _ in range(sizeNum):
x = random.randint(MINNUM, MAXNUM)
array.append(x)
colourArray = ['turquoise' for i in range(len(array))]
drawArray(array, colourArray)
def startAlgorithm():
global array
if not array:
return
# get the speed of the algorithm visualiser
sizeNum = sizeEntry.get()
speed = 10/(sizeNum*pow(sizeNum, 0.5))
# visualise an algorithm
if algoMenu.get() == "Insertionsort":
insertion_sort(array, drawArray, speed)
if algoMenu.get() == "Mergesort":
merge_sort(array, drawArray, speed)
if algoMenu.get() == "Bubblesort":
bubble_sort(array, drawArray, speed)
if algoMenu.get() == "Quicksort":
quick_sort(array, drawArray, speed)
if algoMenu.get() == "Heapsort":
heap_sort(array, drawArray, speed)
if __name__ == "main":
# Frame/ Where our functionalies are
UI_frame = Frame(window, width = MAXWIDTH, height = 200, bg = "blue")
UI_frame.grid(row=0, column=0, padx=10, pady=5)
# Canvas/Where our algorithm is displayed
canvas = Canvas(window, width = MAXWIDTH, height = 480, bg = "white")
canvas.grid(row=1, column=0, pady=5)
# User Interface Area
# first row of functions
label = Label(UI_frame, text = "Algorithms", bg = "grey")
label.grid(row=0, column=0, padx=5, pady=5)
algoMenu = ttk.Combobox(UI_frame, textvariable = selected_alg, values = algorithms)
algoMenu.current(0)
algoMenu.grid(row=0, column=1, padx=5, pady=5)
generateButton = Button(UI_frame, text = 'Generate', command = generate, bg = "grey", font = ("Helvetica", 9, "bold italic"))
generateButton.grid(row=0, column=2, padx=5, pady=5)
startButton = Button(UI_frame, text = 'Start', command = startAlgorithm, bg = "grey", font = ("Helvetica", 9, "bold italic"))
startButton.grid(row=0, column=4, padx=5, pady=5)
# second row of functions
sizeEntry = Scale(UI_frame, from_ =1, to=200, length = 200, digits=3, resolution = 0.1, orient= HORIZONTAL, label= "Array Size and Speed:")
sizeEntry.grid(row=1, column=1, padx=200, pady=5)
sizeEntry.set(100)
window.mainloop()