Spaces:
Configuration error
Configuration error
Commit
·
2918f61
0
Parent(s):
add streamlit
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
from algorithms import insertion_sort
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
st.title("Insertion Sort Visualizer")
|
| 8 |
+
|
| 9 |
+
option = st.radio("Choose input method:", ["Manual Input", "Random List"])
|
| 10 |
+
|
| 11 |
+
if option == "Manual Input":
|
| 12 |
+
user_input = st.text_input("Enter numbers seperated by commas (like 5,3,1,4)")
|
| 13 |
+
if user_input:
|
| 14 |
+
data = list(map(int, user_input.split(',')))
|
| 15 |
+
else:
|
| 16 |
+
length = st.slider("List length", 5,20,8)
|
| 17 |
+
data = random.sample(range(1,30), length)
|
| 18 |
+
|
| 19 |
+
if 'data' in locals() and st.button("Visualize Insertion Sort"):
|
| 20 |
+
steps = insertion_sort(data)
|
| 21 |
+
|
| 22 |
+
st.write(f"Total steps: {len(steps)}")
|
| 23 |
+
|
| 24 |
+
for i, step in enumerate(steps):
|
| 25 |
+
fig, ax = plt.subplots()
|
| 26 |
+
ax.bar(range(len(step)), step)
|
| 27 |
+
ax.set_title(f"Step {i + 1}")
|
| 28 |
+
st.pyplot(fig)
|
| 29 |
+
time.sleep(0.3)
|