|
import streamlit as st |
|
import time |
|
from PIL import Image |
|
|
|
|
|
def bubble_sort(arr): |
|
steps = [] |
|
n = len(arr) |
|
for i in range(n): |
|
for j in range(0, n - i - 1): |
|
if arr[j] > arr[j + 1]: |
|
arr[j], arr[j + 1] = arr[j + 1], arr[j] |
|
steps.append(list(arr)) |
|
return steps |
|
|
|
|
|
def insertion_sort(arr): |
|
steps = [] |
|
for i in range(1, len(arr)): |
|
key = arr[i] |
|
j = i - 1 |
|
while j >= 0 and key < arr[j]: |
|
arr[j + 1] = arr[j] |
|
j -= 1 |
|
steps.append(list(arr)) |
|
arr[j + 1] = key |
|
steps.append(list(arr)) |
|
return steps |
|
|
|
|
|
def selection_sort(arr): |
|
steps = [] |
|
n = len(arr) |
|
for i in range(n): |
|
min_index = i |
|
for j in range(i + 1, n): |
|
if arr[j] < arr[min_index]: |
|
min_index = j |
|
arr[i], arr[min_index] = arr[min_index], arr[i] |
|
steps.append(list(arr)) |
|
return steps |
|
|
|
|
|
def animate_sorting(steps): |
|
for step in steps: |
|
st.write(step) |
|
time.sleep(0.5) |
|
|
|
|
|
def main(): |
|
|
|
st.sidebar.title("Sorting Algorithm Options") |
|
option = st.sidebar.radio("Choose an option:", ("Detailed Explanation", "Try Simulation")) |
|
|
|
if option == "Try Simulation": |
|
st.title("Sorting Algorithms Visualization") |
|
|
|
|
|
user_input = st.text_input("Enter a list of numbers (comma separated):", "64, 34, 25, 12, 22, 11, 90") |
|
|
|
|
|
if user_input: |
|
try: |
|
arr = [int(x) for x in user_input.split(',')] |
|
except ValueError: |
|
st.error("Please enter a valid list of integers.") |
|
return |
|
|
|
|
|
algorithm = st.selectbox("Select sorting algorithm:", ("Bubble Sort", "Insertion Sort", "Selection Sort")) |
|
|
|
|
|
if st.button('Sort'): |
|
steps = [] |
|
if algorithm == "Bubble Sort": |
|
steps = bubble_sort(arr) |
|
elif algorithm == "Insertion Sort": |
|
steps = insertion_sort(arr) |
|
elif algorithm == "Selection Sort": |
|
steps = selection_sort(arr) |
|
|
|
|
|
animate_sorting(steps) |
|
|
|
|
|
st.write("Sorted Array:", arr) |
|
|
|
elif option == "Detailed Explanation": |
|
st.title("Detailed Explanation of Sorting Algorithms") |
|
|
|
|
|
algorithm = st.selectbox("Select an algorithm to see the explanation:", |
|
("Bubble Sort", "Insertion Sort", "Selection Sort")) |
|
|
|
if algorithm == "Bubble Sort": |
|
st.subheader("Bubble Sort Explanation") |
|
st.write(""" |
|
Bubble Sort is a simple sorting algorithm that works by repeatedly stepping through the list, |
|
comparing adjacent elements and swapping them if they are in the wrong order. The pass through the |
|
list is repeated until the list is sorted. It gets its name because the largest unsorted element |
|
"bubbles" to its correct position in each pass. |
|
""") |
|
st.write(""" |
|
**Steps**: |
|
1. Compare adjacent elements. |
|
2. Swap them if they are in the wrong order. |
|
3. Repeat this for each pair of adjacent elements in the array. |
|
4. Continue until no more swaps are needed. |
|
""") |
|
|
|
image = Image.open("bubble-short.png") |
|
st.image(image, use_container_width=True) |
|
|
|
elif algorithm == "Insertion Sort": |
|
st.subheader("Insertion Sort Explanation") |
|
st.write(""" |
|
Insertion Sort is a simple comparison-based algorithm. It builds the sorted array one item at a time. |
|
It works by picking the next item from the unsorted part of the array and inserting it into its correct position |
|
in the sorted part of the array. The process is repeated until the whole array is sorted. |
|
""") |
|
st.write(""" |
|
**Steps**: |
|
1. Start with the second element. |
|
2. Compare it with the elements before it, and move those elements one position to the right if necessary. |
|
3. Insert the element at the correct position. |
|
4. Continue this process until all elements are inserted in the correct order. |
|
""") |
|
|
|
image = Image.open("insertion-sort.png") |
|
st.image(image, use_container_width=True) |
|
|
|
elif algorithm == "Selection Sort": |
|
st.subheader("Selection Sort Explanation") |
|
st.write(""" |
|
Selection Sort is a simple comparison-based algorithm that divides the array into two parts: the sorted part and |
|
the unsorted part. In each pass, it selects the smallest (or largest, depending on the sorting order) element from |
|
the unsorted part and swaps it with the first element of the unsorted part. This process is repeated until the array is sorted. |
|
""") |
|
st.write(""" |
|
**Steps**: |
|
1. Find the smallest element in the unsorted part of the array. |
|
2. Swap it with the first unsorted element. |
|
3. Move the boundary of the sorted part one element forward. |
|
4. Repeat this process for all elements. |
|
""") |
|
|
|
image = Image.open("selection-short.png") |
|
st.image(image, use_container_width=True) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|