Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
| 8 |
+
steps = []
|
| 9 |
+
n = len(arr)
|
| 10 |
+
for i in range(n):
|
| 11 |
+
for j in range(0, n - i - 1):
|
| 12 |
+
if arr[j] > arr[j + 1]:
|
| 13 |
+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
|
| 14 |
+
steps.append(list(arr)) # Record the state of the array after each step
|
| 15 |
+
return steps
|
| 16 |
+
|
| 17 |
+
# Insertion Sort Algorithm with animation
|
| 18 |
+
def insertion_sort(arr):
|
| 19 |
+
steps = []
|
| 20 |
+
for i in range(1, len(arr)):
|
| 21 |
+
key = arr[i]
|
| 22 |
+
j = i - 1
|
| 23 |
+
while j >= 0 and key < arr[j]:
|
| 24 |
+
arr[j + 1] = arr[j]
|
| 25 |
+
j -= 1
|
| 26 |
+
steps.append(list(arr)) # Record the state of the array after each step
|
| 27 |
+
arr[j + 1] = key
|
| 28 |
+
steps.append(list(arr)) # Record the state after inserting the key
|
| 29 |
+
return steps
|
| 30 |
+
|
| 31 |
+
# Selection Sort Algorithm with animation
|
| 32 |
+
def selection_sort(arr):
|
| 33 |
+
steps = []
|
| 34 |
+
n = len(arr)
|
| 35 |
+
for i in range(n):
|
| 36 |
+
min_index = i
|
| 37 |
+
for j in range(i + 1, n):
|
| 38 |
+
if arr[j] < arr[min_index]:
|
| 39 |
+
min_index = j
|
| 40 |
+
arr[i], arr[min_index] = arr[min_index], arr[i]
|
| 41 |
+
steps.append(list(arr)) # Record the state after each swap
|
| 42 |
+
return steps
|
| 43 |
+
|
| 44 |
+
# Quick Sort Algorithm with animation
|
| 45 |
+
def quick_sort(arr):
|
| 46 |
+
steps = []
|
| 47 |
+
def quick_sort_helper(arr, steps):
|
| 48 |
+
if len(arr) <= 1:
|
| 49 |
+
return arr, steps
|
| 50 |
+
pivot = arr[len(arr) // 2]
|
| 51 |
+
left = [x for x in arr if x < pivot]
|
| 52 |
+
middle = [x for x in arr if x == pivot]
|
| 53 |
+
right = [x for x in arr if x > pivot]
|
| 54 |
+
|
| 55 |
+
steps.append(list(arr)) # Record the state before sorting the partitions
|
| 56 |
+
left_sorted, steps = quick_sort_helper(left, steps)
|
| 57 |
+
right_sorted, steps = quick_sort_helper(right, steps)
|
| 58 |
+
|
| 59 |
+
return left_sorted + middle + right_sorted, steps
|
| 60 |
+
|
| 61 |
+
sorted_arr, steps = quick_sort_helper(arr, steps)
|
| 62 |
+
return steps
|
| 63 |
+
|
| 64 |
+
# Function to handle animation in the streamlit app
|
| 65 |
+
def animate_sorting(steps):
|
| 66 |
+
for step in steps:
|
| 67 |
+
st.write(step)
|
| 68 |
+
time.sleep(0.5) # Adjust sleep time to control animation speed
|
| 69 |
+
|
| 70 |
+
# Streamlit App Interface
|
| 71 |
+
def main():
|
| 72 |
+
st.title("Sorting Algorithms Visualization")
|
| 73 |
+
|
| 74 |
+
# Input array from user
|
| 75 |
+
user_input = st.text_input("Enter a list of numbers (comma separated):", "64, 34, 25, 12, 22, 11, 90")
|
| 76 |
+
|
| 77 |
+
# Convert the input into a list of integers
|
| 78 |
+
if user_input:
|
| 79 |
+
try:
|
| 80 |
+
arr = [int(x) for x in user_input.split(',')]
|
| 81 |
+
except ValueError:
|
| 82 |
+
st.error("Please enter a valid list of integers.")
|
| 83 |
+
return
|
| 84 |
+
|
| 85 |
+
# Select the sorting algorithm
|
| 86 |
+
algorithm = st.selectbox("Select sorting algorithm:", ("Bubble Sort", "Insertion Sort", "Selection Sort", "Quick Sort"))
|
| 87 |
+
|
| 88 |
+
# Start the animation on button press
|
| 89 |
+
if st.button('Sort'):
|
| 90 |
+
steps = []
|
| 91 |
+
if algorithm == "Bubble Sort":
|
| 92 |
+
steps = bubble_sort(arr)
|
| 93 |
+
elif algorithm == "Insertion Sort":
|
| 94 |
+
steps = insertion_sort(arr)
|
| 95 |
+
elif algorithm == "Selection Sort":
|
| 96 |
+
steps = selection_sort(arr)
|
| 97 |
+
elif algorithm == "Quick Sort":
|
| 98 |
+
steps = quick_sort(arr)
|
| 99 |
+
|
| 100 |
+
# Animate the sorting process
|
| 101 |
+
animate_sorting(steps)
|
| 102 |
+
|
| 103 |
+
# Display the final sorted array
|
| 104 |
+
st.write("Sorted Array:", arr)
|
| 105 |
+
|
| 106 |
+
# Run the app
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
main()
|