Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
| 3 |
-
import random
|
| 4 |
-
import numpy as np
|
| 5 |
|
| 6 |
# Bubble Sort Algorithm with animation
|
| 7 |
def bubble_sort(arr):
|
|
@@ -49,37 +47,95 @@ def animate_sorting(steps):
|
|
| 49 |
|
| 50 |
# Streamlit App Interface
|
| 51 |
def main():
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
try:
|
| 60 |
-
arr = [int(x) for x in user_input.split(',')]
|
| 61 |
-
except ValueError:
|
| 62 |
-
st.error("Please enter a valid list of integers.")
|
| 63 |
-
return
|
| 64 |
-
|
| 65 |
-
# Select the sorting algorithm
|
| 66 |
-
algorithm = st.selectbox("Select sorting algorithm:", ("Bubble Sort", "Insertion Sort", "Selection Sort"))
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
if
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Run the app
|
| 85 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Bubble Sort Algorithm with animation
|
| 5 |
def bubble_sort(arr):
|
|
|
|
| 47 |
|
| 48 |
# Streamlit App Interface
|
| 49 |
def main():
|
| 50 |
+
# Sidebar with two options: "Try Simulation" and "Detailed Explanation"
|
| 51 |
+
st.sidebar.title("Sorting Algorithm Options")
|
| 52 |
+
option = st.sidebar.radio("Choose an option:", ("Try Simulation", "Detailed Explanation"))
|
| 53 |
|
| 54 |
+
if option == "Try Simulation":
|
| 55 |
+
st.title("Sorting Algorithms Visualization")
|
| 56 |
+
|
| 57 |
+
# Input array from user
|
| 58 |
+
user_input = st.text_input("Enter a list of numbers (comma separated):", "64, 34, 25, 12, 22, 11, 90")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Convert the input into a list of integers
|
| 61 |
+
if user_input:
|
| 62 |
+
try:
|
| 63 |
+
arr = [int(x) for x in user_input.split(',')]
|
| 64 |
+
except ValueError:
|
| 65 |
+
st.error("Please enter a valid list of integers.")
|
| 66 |
+
return
|
| 67 |
+
|
| 68 |
+
# Select the sorting algorithm
|
| 69 |
+
algorithm = st.selectbox("Select sorting algorithm:", ("Bubble Sort", "Insertion Sort", "Selection Sort"))
|
| 70 |
+
|
| 71 |
+
# Start the animation on button press
|
| 72 |
+
if st.button('Sort'):
|
| 73 |
+
steps = []
|
| 74 |
+
if algorithm == "Bubble Sort":
|
| 75 |
+
steps = bubble_sort(arr)
|
| 76 |
+
elif algorithm == "Insertion Sort":
|
| 77 |
+
steps = insertion_sort(arr)
|
| 78 |
+
elif algorithm == "Selection Sort":
|
| 79 |
+
steps = selection_sort(arr)
|
| 80 |
+
|
| 81 |
+
# Animate the sorting process
|
| 82 |
+
animate_sorting(steps)
|
| 83 |
|
| 84 |
+
# Display the final sorted array
|
| 85 |
+
st.write("Sorted Array:", arr)
|
| 86 |
+
|
| 87 |
+
elif option == "Detailed Explanation":
|
| 88 |
+
st.title("Detailed Explanation of Sorting Algorithms")
|
| 89 |
+
|
| 90 |
+
# Show explanations for each algorithm
|
| 91 |
+
algorithm = st.selectbox("Select an algorithm to see the explanation:",
|
| 92 |
+
("Bubble Sort", "Insertion Sort", "Selection Sort"))
|
| 93 |
+
|
| 94 |
+
if algorithm == "Bubble Sort":
|
| 95 |
+
st.subheader("Bubble Sort Explanation")
|
| 96 |
+
st.write("""
|
| 97 |
+
Bubble Sort is a simple sorting algorithm that works by repeatedly stepping through the list,
|
| 98 |
+
comparing adjacent elements and swapping them if they are in the wrong order. The pass through the
|
| 99 |
+
list is repeated until the list is sorted. It gets its name because the largest unsorted element
|
| 100 |
+
"bubbles" to its correct position in each pass.
|
| 101 |
+
""")
|
| 102 |
+
st.write("""
|
| 103 |
+
**Steps**:
|
| 104 |
+
1. Compare adjacent elements.
|
| 105 |
+
2. Swap them if they are in the wrong order.
|
| 106 |
+
3. Repeat this for each pair of adjacent elements in the array.
|
| 107 |
+
4. Continue until no more swaps are needed.
|
| 108 |
+
""")
|
| 109 |
+
|
| 110 |
+
elif algorithm == "Insertion Sort":
|
| 111 |
+
st.subheader("Insertion Sort Explanation")
|
| 112 |
+
st.write("""
|
| 113 |
+
Insertion Sort is a simple comparison-based algorithm. It builds the sorted array one item at a time.
|
| 114 |
+
It works by picking the next item from the unsorted part of the array and inserting it into its correct position
|
| 115 |
+
in the sorted part of the array. The process is repeated until the whole array is sorted.
|
| 116 |
+
""")
|
| 117 |
+
st.write("""
|
| 118 |
+
**Steps**:
|
| 119 |
+
1. Start with the second element.
|
| 120 |
+
2. Compare it with the elements before it, and move those elements one position to the right if necessary.
|
| 121 |
+
3. Insert the element at the correct position.
|
| 122 |
+
4. Continue this process until all elements are inserted in the correct order.
|
| 123 |
+
""")
|
| 124 |
|
| 125 |
+
elif algorithm == "Selection Sort":
|
| 126 |
+
st.subheader("Selection Sort Explanation")
|
| 127 |
+
st.write("""
|
| 128 |
+
Selection Sort is a simple comparison-based algorithm that divides the array into two parts: the sorted part and
|
| 129 |
+
the unsorted part. In each pass, it selects the smallest (or largest, depending on the sorting order) element from
|
| 130 |
+
the unsorted part and swaps it with the first element of the unsorted part. This process is repeated until the array is sorted.
|
| 131 |
+
""")
|
| 132 |
+
st.write("""
|
| 133 |
+
**Steps**:
|
| 134 |
+
1. Find the smallest element in the unsorted part of the array.
|
| 135 |
+
2. Swap it with the first unsorted element.
|
| 136 |
+
3. Move the boundary of the sorted part one element forward.
|
| 137 |
+
4. Repeat this process for all elements.
|
| 138 |
+
""")
|
| 139 |
|
| 140 |
# Run the app
|
| 141 |
if __name__ == "__main__":
|