-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisu.py
More file actions
115 lines (90 loc) · 3.57 KB
/
visu.py
File metadata and controls
115 lines (90 loc) · 3.57 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
115
# import the dependencies
import copy
import time
import pandas as pd
import plotly.express as px
from pandas import DataFrame
# import sorting algorithms from our custom sort module
from sort.bubble import bubble_sort
from sort.counting import count_sort
from sort.insertion import insertion_sort
from sort.mergeSort import merge_sort
from sort.quicksort import quicksort
from sort.radix import radix_sort
# sets up and displays a benchmark graph of sorting the given file by the given column
def plot(file: DataFrame, column: str):
# store algorithms to be used
algos = ['Merge Sort', 'QuickSort', 'Bubble Sort', 'Insertion Sort', 'Radix Sort', 'Counting Sort']
# convert the dataframe column into a list
numbers = file[column].tolist()
# make a deep copy of the list to reuse it for each algorithm
numbers_deep = copy.deepcopy(numbers)
# let user know the program started
print("Sorting... Wait until a browser window opens")
# calculate how much time merge sort takes
start = time.time()
merge_sort(numbers)
end = time.time()
merge = (end - start) * 1000
numbers = numbers_deep
# calculate how much time bubble sort takes
start = time.time()
bubble_sort(numbers)
end = time.time()
bubble = (end - start)
numbers = numbers_deep
# calculate how much time counting sort takes
start = time.time()
count_sort(numbers)
end = time.time()
count = (end - start) * 1000
numbers = numbers_deep
# calculate how much time radix sort takes
start = time.time()
radix_sort(numbers)
end = time.time()
radix = (end - start) * 1000
numbers = numbers_deep
# calculate how much time quick sort takes
start = time.time()
quicksort(numbers)
end = time.time()
quick = (end - start)
numbers = numbers_deep
# calculate how much time insertion sort takes
start = time.time()
insertion_sort(numbers)
end = time.time()
insertion = (end - start) * 1000
# store times
times = [merge, quick, bubble, insertion, radix, count]
# create dataframe
df = pd.DataFrame(
{"Sorting Algorithm": ['MergeSort', 'QuickSort', 'BubbleSort', 'Insertion Sort', 'RadixSort', 'Counting Sort'],
"Time Complexity": ['LINEARITHMIC (n log(n))', 'LINEARITHMIC (n log(n))', 'QUADRATIC (n^2)',
'QUADRATIC (n^2)', 'LINEAR (n)', 'LINEAR (n)'],
"Time": times,
})
# create the figure
"""
'x' and 'y' tell what info to store on the axis
'labels' allows you to name the axis
'hover_name' allows to to store a title for the bar when it is hovered over
"""
fig = px.bar(df, x=algos, y=times, color='Sorting Algorithm',
labels={'x': 'Sorting Algorithm', 'y': 'Time (ms)'},
hover_name='Time Complexity',
color_discrete_sequence=["lightseagreen", "lightseagreen", "lightskyblue", "lightskyblue",
"palevioletred",
"palevioletred"],
)
# add the buttons to hide the chosen algorithm bar charts
fig.update_layout(legend=dict(font=dict(family="Courier", size=17, color="black"), orientation="h",
yanchor="bottom",
y=1.00,
xanchor="right",
x=1),
legend_title="Click to hide the bar chart:",
font=dict(
size=18))
fig.show() # show webpage