Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,35 +11,6 @@ from streamlit.components.v1 import html
|
|
11 |
import matplotlib.colors as mpl
|
12 |
from PIL import Image
|
13 |
|
14 |
-
# Function for bubble sort with step-by-step visualization
|
15 |
-
def bubble_sort(arr, animate=False):
|
16 |
-
n = len(arr)
|
17 |
-
steps = []
|
18 |
-
if animate:
|
19 |
-
steps.append(arr.copy()) # Store initial state for animation
|
20 |
-
for i in range(n):
|
21 |
-
for j in range(0, n-i-1):
|
22 |
-
if arr[j] > arr[j+1]:
|
23 |
-
arr[j], arr[j+1] = arr[j+1], arr[j] # Swap the elements
|
24 |
-
if animate:
|
25 |
-
steps.append(arr.copy()) # Store each step for animation
|
26 |
-
return arr, steps
|
27 |
-
|
28 |
-
# Function to animate the sorting process
|
29 |
-
def animate_sorting(steps):
|
30 |
-
fig, ax = plt.subplots()
|
31 |
-
ax.set_title("Bubble Sort Animation")
|
32 |
-
bar_width = 0.5
|
33 |
-
rects = ax.bar(range(len(steps[0])), steps[0], width=bar_width)
|
34 |
-
|
35 |
-
def update(step):
|
36 |
-
for rect, height in zip(rects, steps[step]):
|
37 |
-
rect.set_height(height)
|
38 |
-
return rects
|
39 |
-
|
40 |
-
ani = animation.FuncAnimation(fig, update, frames=len(steps), repeat=False, interval=300, blit=True)
|
41 |
-
st.pyplot(fig) # Display the animation in Streamlit
|
42 |
-
|
43 |
# Sidebar for selecting an option
|
44 |
sidebar_option = st.sidebar.radio("Select an option",
|
45 |
["Introductory Tutorial", "Basic: Properties",
|
@@ -62,31 +33,6 @@ if sidebar_option == "Introductory Tutorial":
|
|
62 |
# Display a logo or icon
|
63 |
image = Image.open("1.png") # Path to your image file
|
64 |
st.image(image, width=200) # You can adjust the width as needed
|
65 |
-
|
66 |
-
# Add the Bubble Sort Simulator button
|
67 |
-
if st.button("Bubble Sort Simulator"):
|
68 |
-
st.title("Bubble Sort with Animation")
|
69 |
-
|
70 |
-
# Get input array from user
|
71 |
-
user_input = st.text_input("Enter the array elements separated by spaces:")
|
72 |
-
|
73 |
-
# Display the radio button after user input is entered
|
74 |
-
if user_input:
|
75 |
-
arr = list(map(int, user_input.split()))
|
76 |
-
|
77 |
-
# Ask the user if they want an animation
|
78 |
-
animate_option = st.radio("Do you want to see the animation?", options=["Yes", "No"], key="animate_radio")
|
79 |
-
|
80 |
-
# Perform bubble sort and get the steps for animation
|
81 |
-
animate = animate_option == "Yes"
|
82 |
-
sorted_array, steps = bubble_sort(arr, animate)
|
83 |
-
|
84 |
-
# Display the sorted array
|
85 |
-
st.write("Sorted Array:", sorted_array)
|
86 |
-
|
87 |
-
# Show animation if required
|
88 |
-
if animate:
|
89 |
-
animate_sorting(steps)
|
90 |
|
91 |
# Add content descriptions
|
92 |
descriptions = [
|
|
|
11 |
import matplotlib.colors as mpl
|
12 |
from PIL import Image
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Sidebar for selecting an option
|
15 |
sidebar_option = st.sidebar.radio("Select an option",
|
16 |
["Introductory Tutorial", "Basic: Properties",
|
|
|
33 |
# Display a logo or icon
|
34 |
image = Image.open("1.png") # Path to your image file
|
35 |
st.image(image, width=200) # You can adjust the width as needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Add content descriptions
|
38 |
descriptions = [
|