Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
| 3 |
-
import plotly.graph_objs as go
|
| 4 |
-
import plotly.express as px
|
| 5 |
from PIL import Image
|
| 6 |
-
import numpy as np
|
| 7 |
|
| 8 |
# Inject Custom CSS for Aesthetic Enhancements
|
| 9 |
def inject_custom_css():
|
|
@@ -74,7 +71,7 @@ def inject_custom_css():
|
|
| 74 |
</style>
|
| 75 |
""", unsafe_allow_html=True)
|
| 76 |
|
| 77 |
-
# Bubble Sort Algorithm with
|
| 78 |
def bubble_sort(arr):
|
| 79 |
steps = []
|
| 80 |
n = len(arr)
|
|
@@ -85,7 +82,7 @@ def bubble_sort(arr):
|
|
| 85 |
steps.append(list(arr)) # Record the state of the array after each step
|
| 86 |
return steps
|
| 87 |
|
| 88 |
-
# Insertion Sort Algorithm with
|
| 89 |
def insertion_sort(arr):
|
| 90 |
steps = []
|
| 91 |
for i in range(1, len(arr)):
|
|
@@ -99,7 +96,7 @@ def insertion_sort(arr):
|
|
| 99 |
steps.append(list(arr)) # Record the state after inserting the key
|
| 100 |
return steps
|
| 101 |
|
| 102 |
-
# Selection Sort Algorithm with
|
| 103 |
def selection_sort(arr):
|
| 104 |
steps = []
|
| 105 |
n = len(arr)
|
|
@@ -112,37 +109,11 @@ def selection_sort(arr):
|
|
| 112 |
steps.append(list(arr)) # Record the state after each swap
|
| 113 |
return steps
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
def
|
| 117 |
-
fig = go.Figure()
|
| 118 |
for step in steps:
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
y=step,
|
| 122 |
-
mode="lines+markers",
|
| 123 |
-
name=f'{algorithm_name} Sorting Step',
|
| 124 |
-
line=dict(color='blue'),
|
| 125 |
-
marker=dict(color='red', size=10),
|
| 126 |
-
))
|
| 127 |
-
fig.update_layout(
|
| 128 |
-
title=f"{algorithm_name} Algorithm - Sorting Process",
|
| 129 |
-
xaxis_title="Index",
|
| 130 |
-
yaxis_title="Value",
|
| 131 |
-
updatemenus=[dict(
|
| 132 |
-
type="buttons",
|
| 133 |
-
showactive=False,
|
| 134 |
-
buttons=[dict(label="Play",
|
| 135 |
-
method="animate",
|
| 136 |
-
args=[None, dict(frame=dict(duration=500, redraw=True), fromcurrent=True)])]
|
| 137 |
-
)],
|
| 138 |
-
sliders=[dict(
|
| 139 |
-
steps=[dict(label=str(i),
|
| 140 |
-
method="animate",
|
| 141 |
-
args=[[i], dict(frame=dict(duration=500, redraw=True), mode="immediate", transition=dict(duration=0))])
|
| 142 |
-
for i in range(len(steps))])
|
| 143 |
-
]
|
| 144 |
-
)
|
| 145 |
-
st.plotly_chart(fig)
|
| 146 |
|
| 147 |
# Streamlit App Interface
|
| 148 |
def main():
|
|
@@ -183,8 +154,8 @@ def main():
|
|
| 183 |
elif algorithm == "Selection Sort":
|
| 184 |
steps = selection_sort(arr)
|
| 185 |
|
| 186 |
-
#
|
| 187 |
-
|
| 188 |
|
| 189 |
# Display the final sorted array
|
| 190 |
st.write("Sorted Array:", arr)
|
|
@@ -253,4 +224,4 @@ def main():
|
|
| 253 |
|
| 254 |
# Run the app
|
| 255 |
if __name__ == "__main__":
|
| 256 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
# Inject Custom CSS for Aesthetic Enhancements
|
| 6 |
def inject_custom_css():
|
|
|
|
| 71 |
</style>
|
| 72 |
""", unsafe_allow_html=True)
|
| 73 |
|
| 74 |
+
# Bubble Sort Algorithm with animation
|
| 75 |
def bubble_sort(arr):
|
| 76 |
steps = []
|
| 77 |
n = len(arr)
|
|
|
|
| 82 |
steps.append(list(arr)) # Record the state of the array after each step
|
| 83 |
return steps
|
| 84 |
|
| 85 |
+
# Insertion Sort Algorithm with animation
|
| 86 |
def insertion_sort(arr):
|
| 87 |
steps = []
|
| 88 |
for i in range(1, len(arr)):
|
|
|
|
| 96 |
steps.append(list(arr)) # Record the state after inserting the key
|
| 97 |
return steps
|
| 98 |
|
| 99 |
+
# Selection Sort Algorithm with animation
|
| 100 |
def selection_sort(arr):
|
| 101 |
steps = []
|
| 102 |
n = len(arr)
|
|
|
|
| 109 |
steps.append(list(arr)) # Record the state after each swap
|
| 110 |
return steps
|
| 111 |
|
| 112 |
+
# Function to handle animation in the streamlit app
|
| 113 |
+
def animate_sorting(steps):
|
|
|
|
| 114 |
for step in steps:
|
| 115 |
+
st.write(step)
|
| 116 |
+
time.sleep(0.5) # Adjust sleep time to control animation speed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Streamlit App Interface
|
| 119 |
def main():
|
|
|
|
| 154 |
elif algorithm == "Selection Sort":
|
| 155 |
steps = selection_sort(arr)
|
| 156 |
|
| 157 |
+
# Animate the sorting process
|
| 158 |
+
animate_sorting(steps)
|
| 159 |
|
| 160 |
# Display the final sorted array
|
| 161 |
st.write("Sorted Array:", arr)
|
|
|
|
| 224 |
|
| 225 |
# Run the app
|
| 226 |
if __name__ == "__main__":
|
| 227 |
+
main()
|