An interactive web application built with HTML, Tailwind CSS, and pure JavaScript to simultaneously visualize and compare the performance and operational patterns of six fundamental sorting algorithms.
- Concurrent Visualization: Watch Bubble Sort, Insertion Sort, Quick Sort, Selection Sort, Heap Sort, and Merge Sort run side-by-side on the same initial randomized array.
- Clear Animations: Uses distinct color coding to highlight key operations:
- Yellow: Comparison
- Red: Swap Operation
- Violet: Pivot/Key Element
- Green: Permanently Sorted Element
- Speed Control: Adjust the visualization speed from Slow (400ms delay per step) to Turbo (10ms delay per step).
- Code Reference: Each visualizer includes the corresponding pseudocode for the algorithm being executed.
- Pure JavaScript: No heavy frameworks required, making the source code easy to follow for educational purposes.
You only need a modern web browser to run this application.
- Save the file: Save the entire code block provided as an HTML file (e.g.,
index.html). - Open in Browser: Double-click the saved
index.htmlfile to open it in your web browser (Chrome, Firefox, Edge, etc.). - Start: Click the "Start Visualization" button to begin the sorting process.
The core of the application relies on an action-recording pattern to decouple the sorting logic from the animation:
- Action Generation: Each sorting function (
getBubbleSortActions,getQuickSortActions, etc.) takes the initial array and simulates the sorting, recording every significant step (compare, swap, pivot placement) into an array of objects. - Visualization Execution: The
startAllVisualizationsfunction runs all recorded action arrays in parallel. - DOM Manipulation: The
Visualizerclass reads the next action, applies the corresponding color and position changes using CSS transitions (style.orderandclassName), and usessetTimeout/delayto wait for the configuredANIMATION_SPEED_MSbefore processing the next action.
To ensure the visual bars correspond correctly to the logical array indices even after multiple swaps, the following data attributes are used:
data-index: The current logical index (position in the array) for the bar. This is swapped during a visual swap.data-value: The original value of the bar, which remains constant.
You can easily adjust the visualization parameters within the <script> tag:
| Variable | Description | Default Value |
|---|---|---|
ARRAY_SIZE |
The number of bars to display. | 15 |
MAX_VALUE |
The maximum height (percentage) of any bar. | 90 |
ANIMATION_SPEED_MS |
Initial delay between steps in milliseconds. | 150 |