Skip to content

mohammed0xff/SortingVisualization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SortingVisualization

Sort ALgorithms Visualized with SDL2.

Demo

Merge sort

Bubble sort

Selection sort

Insertion sort

Quick sort

Heap sort

Main minu

How to run

On Linux :

  • 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 

On Windows :

  • Open SortingVisualization.sln file
  • Follow lazy foo guide to set up SDL2 on Visual Studio.
  • Press F5 to build and run the program.

Contribution

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;

    };

};

2. Implement your sort function.

  • 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);
    }
}

3. add the pointers refrences to m_pointers array with a chosen color.

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.

5. Any contrubutions are appreaciated, you can have as much fun as you want with this project <3.

To Do :

  • Implement a TextManager class to seperate text rendering management from SortVisualizer class.
  • Implement an EventHandler class with std::unordered_map<T, callback> where T is an event.
  • Make text look prettier.
  • Add sound.

About

Sort ALgorithms Visualized with SDL2.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published