Skip to content

Commit 7f81ef2

Browse files
committed
Introduced Sorting Algorithm Visualizer
- `displayer.py`: Handles the Pygame window, rendering, and visualization updates. - `sound_manager.py`: Contains the `SoundManager` class for generating and playing audio feedback. - `main.py`: The application entry point, setting up the initial visualization with a basic array. - `algorithms/`: Directory structure for algorithms, starting with `bubble_sort.py`. - Includes `requirements.txt` and a basic `README.md`.
1 parent 981878a commit 7f81ef2

File tree

4 files changed

+820
-0
lines changed

4 files changed

+820
-0
lines changed

algorithms/bubble_sort.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def bubble_sort(array, update_callback):
2+
n = len(array)
3+
swapped = True
4+
pass_num = 0
5+
while swapped:
6+
swapped = False
7+
for j in range(0, n - pass_num - 1):
8+
# Highlight compared elements
9+
update_callback(array, highlight_indices=[j, j + 1], moving_index=None)
10+
if array[j] > array[j + 1]:
11+
array[j], array[j + 1] = array[j + 1], array[j]
12+
swapped = True
13+
# Highlight swapped elements, moving_index shows the rightmost element involved
14+
update_callback(array, highlight_indices=[j, j + 1], moving_index=j + 1)
15+
pass_num += 1
16+
if not swapped:
17+
break
18+
19+
update_callback(array) # Final update before sweep
20+
21+
# Final sweep animation
22+
for i in range(n):
23+
update_callback(array, moving_index=i, end=True, sweep=True)

0 commit comments

Comments
 (0)