This repository contains a C++ program that utilizes SDL 2 to animate multiple sorting algorithms.
![]() |
---|
Merge sort, 200 elements, 0 delay (RIP frame rate) |
To run this project, follow these steps:
- Install SDL 2.
- Clone this repository.
- Open a terminal and navigate to the local repository.
- Use the make command to generate the sort executable.
- Run ./sort to start the program.
Note: This project is compatible with Linux systems and Mac OS. On Windows, you can run it using WSL or a virtual machine.
Running ./sort -h
will display additional usage information:
Usage:
./sort -a <algorithm> -s <size> -d <delay>
Algorithms:
bubble
selection
insertion
merge
quick
counting
shell
bogo
Sizes:
10, 25, 50, 100, 200, 300, 600, 1200
Delays:
0 <= delay (ms) <= 1000
Defaults:
algorithm: bubble
size: 200
delay: 0
The program is divided into three components. The main component handles user option parsing, calls the sorting functions, and spawns a thread to keep the window open. The second component manages the SDL 2 features, including window creation, destruction, array drawing, and animation parameter management. The third component handles all the sorting implementations. The main program acts as a controller, invoking both the SDL functions and the sorting functions to execute the program according to user preferences.
-
sort.cpp
:This file contains the main program flow. It starts by assigning the algorithm, array size, bar width, and delay based on the user's command line arguments. Next, it sets up the SDL window and spawns a thread to continuously check if the user has closed the window. While the thread is running, the main thread creates an array with random values, draws it, and calls the appropriate sorting algorithm.
If the user closes the window, the sorting algorithm stops, the window thread is joined, and the main function returns. If the user doesn't close the window, the window thread waits at the
join()
statement before returning. -
sort_functions.h
:This file contains all the sorting functions and a helper function to swap two elements in the array. Each sorting function follows a similar format with respect to the animation. At each crucial step of the sorting process,
draw_array()
is called, highlighting the moving element in red. If thisdraw_array()
call returns false, the sorting function also returns false. This mechanism allows the program to exit the sorting function if the user closes the window and indicates that the array did not finish sorting. At the end of each sorting function, the array is drawn entirely in white. -
SDL_functions.h
:This file defines a class called
animation_data
and functions relating to the SDL window. Theanimation_data
class contains the SDL window, SDL renderer, delay, size, bar spacing, bar width, and a boolean indicating whether the window should be open or not. Three main functions manage the SDL window:SDL_init()
creates the window,SDL_close()
closes the window, anddraw_array()
draws an array on the window.The
draw_array()
function is the main point of interest in this file. It starts by checking theopen
attribute of theanimation_data
object. If the user wants the window open (open == true
), the function proceeds. To draw the array, it first clears the screen by drawing a black box, and then loops through the array, drawing a white rectangle sized corresponding to the value of that element. The function then sleeps for the specified delay time, renders the changes, and returns.