Update app.py
Browse files
app.py
CHANGED
@@ -41,26 +41,6 @@ def selection_sort(arr):
|
|
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:
|
@@ -83,7 +63,7 @@ def main():
|
|
83 |
return
|
84 |
|
85 |
# Select the sorting algorithm
|
86 |
-
algorithm = st.selectbox("Select sorting algorithm:", ("Bubble Sort", "Insertion Sort", "Selection Sort"
|
87 |
|
88 |
# Start the animation on button press
|
89 |
if st.button('Sort'):
|
@@ -94,8 +74,6 @@ def main():
|
|
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)
|
|
|
41 |
steps.append(list(arr)) # Record the state after each swap
|
42 |
return steps
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# Function to handle animation in the streamlit app
|
45 |
def animate_sorting(steps):
|
46 |
for step in steps:
|
|
|
63 |
return
|
64 |
|
65 |
# Select the sorting algorithm
|
66 |
+
algorithm = st.selectbox("Select sorting algorithm:", ("Bubble Sort", "Insertion Sort", "Selection Sort"))
|
67 |
|
68 |
# Start the animation on button press
|
69 |
if st.button('Sort'):
|
|
|
74 |
steps = insertion_sort(arr)
|
75 |
elif algorithm == "Selection Sort":
|
76 |
steps = selection_sort(arr)
|
|
|
|
|
77 |
|
78 |
# Animate the sorting process
|
79 |
animate_sorting(steps)
|