demo.mp4
Developed in C with SDL3. This small project allows you to visualize how different sorting algorithms work, like those youtube videos we all have seen. This is not the first one nor the best one, but this one is in C and that is something. Includes:
The algorithms are implemented in src/algorithms.c and the visualizer is in
src/main.c.
To compile and run the visualizer you will need to have
SDL3 installed, as well as make and
gcc installed. To compile, just run:
make
./mainThe visualizer can be configured in src/config.h. Options are:
WIDTHandHEIGHTfor the window sizeUS_STEPbetween updates for the algorithmsLIST_SIZEfor the size of the listSPACINGfor the spacing between barsMIN_FREQfor the frequency of the lowest elementMAX_FREQfor the frequency of the highest element
Once configured, the code must be compiled again with make.
The new algorithm must be implemented in src/algorithms.c and declared in
src/algorithms.h. The algorithms are executed in a separate thread, which has
the advantage of us being able to reuse code from the algorithms directly. The
only change needed is a call to the step(info, red_index, green_index1, green_index2, sound_index) every time we want the visualizer to show a change
(be it a swap, a comparison...). Green can be used to indicate the compared
elements, and red to show a pivot or similar. sound_index is the index of the
element that should be played as a sound (bigger element, higher pitch).
Adding a new algorithm to the code is relatively simple. Steps to follow are:
- Add the algorithm declaration to
src/algorithms.hand the implementation tosrc/algorithms.c - Add the name of the algorithm to the
Stateenum insrc/main.c - In the switch statement in the function
SDL_AppEventadd the key handler for the new algorithm:
/* In main.c */
switch (event->key.key) {
/* ... */
case SDLK_8:
state = NEW_SORT;
updateThread();
break;
case SDLK_ESCAPE:
/* ... */
}- In the function
showMenuadd the new algorithm to the types list. - Finally, in the function
threadFunctionadd the new algorithm to theswitchstatement:
/* In main.c */
switch (state) {
/* ... */
case NEW_SORT:
new_sort(&info);
break;
/* ... */
}- Controls for speed and number of bars
- Add more algorithms
- Use SDL3_ttf??