|
1 | | -# sorting-algorithm-visualizer |
2 | | -A Pygame application to visualize sorting algorithms with animation, sound, and interactive controls. |
| 1 | +# Sorting Algorithm Visualizer |
| 2 | + |
| 3 | +[](https://www.python.org/) |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | +[](https://github.com/psf/black) |
| 6 | + |
| 7 | +A Python application using Pygame to visualize various sorting algorithms in action. Features include an interactive startup menu, adjustable speed, sound feedback for element access/movement, and fullscreen support. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +* **Interactive Startup Menu:** Configure the visualization (algorithm, array size, max value, speed, uniqueness) through a simple text menu when the application starts. |
| 14 | +* **Multiple Algorithms:** Visualize classic sorting algorithms. |
| 15 | +* **Visual Feedback:** Bars represent array elements, colored based on value and status (comparing, moving, sorted). |
| 16 | +* **Auditory Feedback:** Distinct sounds play when elements are accessed or moved, with pitch corresponding to the element's value. |
| 17 | +* **Speed Control:** Adjust the visualization speed dynamically using keyboard shortcuts *during* the visualization. |
| 18 | +* **Pause/Resume:** Pause the visualization at any point. |
| 19 | +* **Fullscreen Mode:** Toggle between windowed and fullscreen display. |
| 20 | +* **Dynamic Algorithm Loading:** Easily add new sorting algorithms by placing them in the `algorithms` directory. |
| 21 | +* **Robust Error Handling:** Gracefully handles common errors during setup and visualization. |
| 22 | + |
| 23 | +## Available Algorithms |
| 24 | + |
| 25 | +The visualizer automatically detects algorithms placed in the `algorithms/` directory. Currently included: |
| 26 | + |
| 27 | +* `bubble_sort` |
| 28 | +* `selection_sort` |
| 29 | +* `insertion_sort` |
| 30 | +* `quick_sort` |
| 31 | +* `heap_sort` |
| 32 | +* `radix_sort` |
| 33 | +* `merge_sort` |
| 34 | +* `shell_sort` |
| 35 | + |
| 36 | +## Requirements |
| 37 | + |
| 38 | +* Python 3.7+ |
| 39 | +* Pygame: `pip install pygame` |
| 40 | +* NumPy: `pip install numpy` |
| 41 | + |
| 42 | +You can install all requirements using: |
| 43 | +```bash |
| 44 | +pip install -r requirements.txt |
| 45 | +``` |
| 46 | +*(Ensure you have a `requirements.txt` file containing `pygame` and `numpy`)* |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +1. **Navigate:** Open your terminal or command prompt and navigate to the project's root directory (the one containing `main.py` and the `algorithms/` folder). |
| 51 | +2. **Run:** Execute the main script: |
| 52 | + ```bash |
| 53 | + python main.py |
| 54 | + ``` |
| 55 | +3. **Configure:** Follow the prompts in the terminal to: |
| 56 | + * Choose the sorting algorithm from the list. |
| 57 | + * Enter the desired array size (number of elements). |
| 58 | + * Enter the maximum value for elements in the array. |
| 59 | + * Enter the initial delay between visualization steps (in milliseconds - lower is faster). |
| 60 | + * Choose whether to generate unique elements. |
| 61 | +4. **Visualize:** Once configured, the Pygame window will launch and the visualization will begin. |
| 62 | + |
| 63 | +## Controls (During Visualization) |
| 64 | + |
| 65 | +* `+` / `=` / Numpad `+`: Increase delay (slow down). |
| 66 | +* `-` / Numpad `-`: Decrease delay (speed up). |
| 67 | +* `P`: Pause / Resume the visualization. |
| 68 | +* `ESC`: Toggle fullscreen mode. |
| 69 | +* `R`: Restart the current visualization with the same settings. |
| 70 | +* `Q`: Quit the application immediately. |
| 71 | + |
| 72 | +## Adding New Algorithms |
| 73 | + |
| 74 | +1. Create a new Python file in the `algorithms/` directory (e.g., `my_cool_sort.py`). |
| 75 | +2. Inside the file, define a function with the *exact same name* as the file (e.g., `def my_cool_sort(array, update_callback):`). |
| 76 | +3. Implement your sorting algorithm within this function. |
| 77 | +4. Call the `update_callback` function whenever you want the display to refresh. Pass the current state of the `array` and optionally `highlight_indices` (list of indices to color differently) and `moving_index` (index of the element currently being moved/placed/compared). Use `sweep=True` in the final pass. |
| 78 | + * `update_callback(array, highlight_indices=[i, j], moving_index=k)` |
| 79 | + * `update_callback(array, moving_index=i, end=True, sweep=True)` # For final sweep |
| 80 | +5. The new algorithm (`my_cool_sort`) will automatically appear in the startup menu the next time you run `python main.py`. |
| 81 | + |
| 82 | +## License |
| 83 | + |
| 84 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments