|
import streamlit as st |
|
import random |
|
from colors import * |
|
from algorithms.bubbleSort import bubble_sort |
|
from algorithms.mergeSort import merge_sort |
|
from algorithms.selectionSort import selection_sort |
|
from algorithms.insertionSort import insertion_sort |
|
from algorithms.quickSort import quick_sort |
|
from algorithms.countingSort import counting_sort |
|
from algorithms.radixSort import radix_sort |
|
from algorithms.heapSort import heap_sort |
|
from algorithms.bucketSort import bucket_sort |
|
from algorithms.shellSort import shell_sort |
|
|
|
|
|
st.set_page_config(page_title='Sorting Algorithms Visualization', layout='wide') |
|
st.title("Sorting Algorithms Visualization") |
|
|
|
|
|
data = [] |
|
|
|
def draw_data(data, color_array): |
|
""" |
|
Function to display the data on Streamlit. |
|
""" |
|
st.write("### Sorting Visualization") |
|
|
|
|
|
chart_data = {"Index": list(range(len(data))), "Value": data} |
|
|
|
|
|
chart = st.bar_chart(chart_data, use_container_width=True) |
|
|
|
def generate(): |
|
""" |
|
Function to generate random data. |
|
""" |
|
global data |
|
data = [random.randint(1, 150) for _ in range(100)] |
|
draw_data(data, [BLUE for _ in range(len(data))]) |
|
|
|
def set_speed(): |
|
""" |
|
Function to set speed based on the selected speed level. |
|
""" |
|
speed = st.selectbox('Select Sorting Speed', ['Fast', 'Medium', 'Slow']) |
|
if speed == 'Slow': |
|
return 0.3 |
|
elif speed == 'Medium': |
|
return 0.1 |
|
else: |
|
return 0.001 |
|
|
|
def sort(): |
|
""" |
|
Function to sort the data based on selected algorithm. |
|
""" |
|
global data |
|
time_tick = set_speed() |
|
algorithm = st.selectbox('Select Sorting Algorithm', [ |
|
'Bubble Sort', |
|
'Merge Sort', |
|
'Selection Sort', |
|
'Insertion Sort', |
|
'Quick Sort', |
|
'Counting Sort', |
|
'Radix Sort', |
|
'Heap Sort', |
|
'Bucket Sort', |
|
'Shell Sort' |
|
]) |
|
|
|
if algorithm == 'Bubble Sort': |
|
bubble_sort(data, draw_data, time_tick) |
|
elif algorithm == 'Merge Sort': |
|
merge_sort(data, 0, len(data)-1, draw_data, time_tick) |
|
elif algorithm == 'Selection Sort': |
|
selection_sort(data, draw_data, time_tick) |
|
elif algorithm == 'Insertion Sort': |
|
insertion_sort(data, draw_data, time_tick) |
|
elif algorithm == 'Quick Sort': |
|
quick_sort(data, 0, len(data)-1, draw_data, time_tick) |
|
elif algorithm == 'Counting Sort': |
|
counting_sort(data, draw_data, time_tick) |
|
elif algorithm == 'Radix Sort': |
|
radix_sort(data, draw_data, time_tick) |
|
elif algorithm == 'Heap Sort': |
|
heap_sort(data, draw_data, time_tick) |
|
elif algorithm == 'Bucket Sort': |
|
bucket_sort(data, len(data), draw_data, time_tick) |
|
elif algorithm == 'Shell Sort': |
|
shell_sort(data, draw_data, time_tick) |
|
|
|
|
|
if st.button('Generate Array'): |
|
generate() |
|
|
|
if st.button('Sort'): |
|
sort() |
|
|
|
|