Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import random
|
3 |
+
from colors import *
|
4 |
+
from algorithms.bubbleSort import bubble_sort
|
5 |
+
from algorithms.mergeSort import merge_sort
|
6 |
+
from algorithms.selectionSort import selection_sort
|
7 |
+
from algorithms.insertionSort import insertion_sort
|
8 |
+
from algorithms.quickSort import quick_sort
|
9 |
+
from algorithms.countingSort import counting_sort
|
10 |
+
from algorithms.radixSort import radix_sort
|
11 |
+
from algorithms.heapSort import heap_sort
|
12 |
+
from algorithms.bucketSort import bucket_sort
|
13 |
+
from algorithms.shellSort import shell_sort
|
14 |
+
|
15 |
+
# Create a Streamlit app
|
16 |
+
st.set_page_config(page_title='Sorting Algorithms Visualization', layout='wide')
|
17 |
+
st.title("Sorting Algorithms Visualization")
|
18 |
+
|
19 |
+
# Initialize the data list
|
20 |
+
data = []
|
21 |
+
|
22 |
+
def draw_data(data, color_array):
|
23 |
+
"""
|
24 |
+
Function to display the data on Streamlit.
|
25 |
+
"""
|
26 |
+
st.write("### Sorting Visualization")
|
27 |
+
|
28 |
+
# Convert data to a chart-friendly format
|
29 |
+
chart_data = {"Index": list(range(len(data))), "Value": data}
|
30 |
+
|
31 |
+
# Display the bar chart using Streamlit's built-in plotting
|
32 |
+
chart = st.bar_chart(chart_data, use_container_width=True)
|
33 |
+
|
34 |
+
def generate():
|
35 |
+
"""
|
36 |
+
Function to generate random data.
|
37 |
+
"""
|
38 |
+
global data
|
39 |
+
data = [random.randint(1, 150) for _ in range(100)]
|
40 |
+
draw_data(data, [BLUE for _ in range(len(data))])
|
41 |
+
|
42 |
+
def set_speed():
|
43 |
+
"""
|
44 |
+
Function to set speed based on the selected speed level.
|
45 |
+
"""
|
46 |
+
speed = st.selectbox('Select Sorting Speed', ['Fast', 'Medium', 'Slow'])
|
47 |
+
if speed == 'Slow':
|
48 |
+
return 0.3
|
49 |
+
elif speed == 'Medium':
|
50 |
+
return 0.1
|
51 |
+
else:
|
52 |
+
return 0.001
|
53 |
+
|
54 |
+
def sort():
|
55 |
+
"""
|
56 |
+
Function to sort the data based on selected algorithm.
|
57 |
+
"""
|
58 |
+
global data
|
59 |
+
time_tick = set_speed()
|
60 |
+
algorithm = st.selectbox('Select Sorting Algorithm', [
|
61 |
+
'Bubble Sort',
|
62 |
+
'Merge Sort',
|
63 |
+
'Selection Sort',
|
64 |
+
'Insertion Sort',
|
65 |
+
'Quick Sort',
|
66 |
+
'Counting Sort',
|
67 |
+
'Radix Sort',
|
68 |
+
'Heap Sort',
|
69 |
+
'Bucket Sort',
|
70 |
+
'Shell Sort'
|
71 |
+
])
|
72 |
+
|
73 |
+
if algorithm == 'Bubble Sort':
|
74 |
+
bubble_sort(data, draw_data, time_tick)
|
75 |
+
elif algorithm == 'Merge Sort':
|
76 |
+
merge_sort(data, 0, len(data)-1, draw_data, time_tick)
|
77 |
+
elif algorithm == 'Selection Sort':
|
78 |
+
selection_sort(data, draw_data, time_tick)
|
79 |
+
elif algorithm == 'Insertion Sort':
|
80 |
+
insertion_sort(data, draw_data, time_tick)
|
81 |
+
elif algorithm == 'Quick Sort':
|
82 |
+
quick_sort(data, 0, len(data)-1, draw_data, time_tick)
|
83 |
+
elif algorithm == 'Counting Sort':
|
84 |
+
counting_sort(data, draw_data, time_tick)
|
85 |
+
elif algorithm == 'Radix Sort':
|
86 |
+
radix_sort(data, draw_data, time_tick)
|
87 |
+
elif algorithm == 'Heap Sort':
|
88 |
+
heap_sort(data, draw_data, time_tick)
|
89 |
+
elif algorithm == 'Bucket Sort':
|
90 |
+
bucket_sort(data, len(data), draw_data, time_tick)
|
91 |
+
elif algorithm == 'Shell Sort':
|
92 |
+
shell_sort(data, draw_data, time_tick)
|
93 |
+
|
94 |
+
# Streamlit buttons and actions
|
95 |
+
if st.button('Generate Array'):
|
96 |
+
generate()
|
97 |
+
|
98 |
+
if st.button('Sort'):
|
99 |
+
sort()
|
100 |
+
|