Sort ALgorithms Visualized with SDL2.
- Install cmake and sdl2.
sudo apt Install cmake libsdl2-dev
- Install sdl2-image
sudo apt install libsdl2-image-dev
- Install sdl2-ttf
sudo apt-get install libsdl2-ttf-dev
- Clone the repo
git clone https://github.com/mohammed0xff/SortingVisualization
- Make n Run
cd SortingVisualization/SortingVisualization
cmake . && make
./sortVisualizer
- Open
SortingVisualization.sln
file - Follow lazy foo guide to set up SDL2 on Visual Studio.
- Press
F5
to build and run the program.
1. Drive from SortVisualizer
class overriding the sort
function, adding pointers and main sort functions.
namespace Visualizer {
class SelectionSortVisulaizer : public SortVisualizer
{
public:
SelectionSortVisulaizer(SDL_Renderer* renderer);
~SelectionSortVisulaizer();
void sort() override;
private:
void addPointers() override;
void selectionSort();
// pointers used for accessing the array
int idxOnePtr;
int idxTwoPtr;
int min_idxPtr;
};
};
- place the rendering function
render()
wherever there is a change to the array. incArrayAccess()
,incComparisons()
to count comparisons and array accesses over the array.
void SelectionSortVisulaizer::sort()
{
selectionSort();
}
void SelectionSortVisulaizer::selectionSort()
{
for (idxOnePtr = startElement; idxOnePtr < endElement; idxOnePtr++)
{
min_idxPtr = idxOnePtr;
for (idxTwoPtr = idxOnePtr + 1; idxTwoPtr < endElement; idxTwoPtr++) {
if (m_arr[idxTwoPtr] < m_arr[min_idxPtr])
min_idxPtr = idxTwoPtr;
incArrayAccess(2);
incComparisons(3);
render();
}
std::swap(m_arr[min_idxPtr], m_arr[idxOnePtr]);
incArrayAccess(2);
}
}
void SelectionSortVisulaizer::addPointers() {
m_pointers.push_back(std::pair<int&, Color>(idxOnePtr, GREEN));
m_pointers.push_back(std::pair<int&, Color>(idxTwoPtr, GREEN));
m_pointers.push_back(std::pair<int&, Color>(min_idxPtr, BLUE));
}
4. Edit Mianminu
class as well as the SortVisualizer.psd
photoshop file to add your new sort algorithm to the main minu.
if you can't do it just ask me and i will do it for you :D.
- Implement a
TextManager
class to seperate text rendering management fromSortVisualizer
class. - Implement an
EventHandler
class withstd::unordered_map<T, callback>
whereT
is an event. - Make text look prettier.
- Add sound.