import streamlit as st import random from colors import * from bubbleSort import bubble_sort from selectionSort import selection_sort from insertionSort import insertion_sort from quickSort import quick_sort # Create a Streamlit app st.set_page_config(page_title='Sorting Algorithms Visualization', layout='wide') st.title("Sorting Algorithms Visualization") # Initialize the data list data = [] def draw_data(data, color_array): """ Function to display the data on Streamlit. """ st.write("### Sorting Visualization") # Convert data to a chart-friendly format chart_data = {"Index": list(range(len(data))), "Value": data} # Display the bar chart using Streamlit's built-in plotting 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', 'Selection Sort', 'Insertion Sort', 'Quick Sort' ]) if algorithm == 'Bubble Sort': bubble_sort(data, 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) # Streamlit buttons and actions if st.button('Generate Array'): generate() if st.button('Sort'): sort()