File size: 2,056 Bytes
8ba85a1
 
 
63dc88c
 
 
 
 
8ba85a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63dc88c
8ba85a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()