Skip to content

Commit

Permalink
v1.0 final
Browse files Browse the repository at this point in the history
  • Loading branch information
alesbe committed Apr 18, 2022
1 parent db0de6c commit e012576
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 57 deletions.
2 changes: 2 additions & 0 deletions 05-sorting-visualizer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@
<ClCompile Include="Sortable.cpp" />
<ClCompile Include="SortAlgorithms.cpp" />
<ClCompile Include="SortController.cpp" />
<ClCompile Include="Utils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Sortable.h" />
<ClInclude Include="SortAlgorithms.h" />
<ClInclude Include="SortController.h" />
<ClInclude Include="Utils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Sorting Visualizer 📚

A lightweight sorting visualizer made with C++ and [SFML](https://www.sfml-dev.org/index.php).

![Bubble sort](https://i.imgur.com/iFIRllR.png) <br>
*Bubble sort*

![Bubble sort info](https://i.imgur.com/lZL6G0a.png) <br>
*Sort info*

## Sort types 🗂️
- Bubble sort
- Selection sort
- Insertion sort

## Usage 🕹️
**F1**: Change number of elements <br>
**F2**: Change time between comparisons <br>
**Arrow Up / Arrow down**: Change sort type <br>
**Space**: Start sort <br>
**Backspace**: Stop sort

## Known issues ❗
- Program stops when a non existing sort type tries to be loaded

## Download 🖨️
- Download pre-compiled exe from [Releases](https://github.com/alesbe/sorting-visualizer/releases). <br>
**Note: The libraries are static, so they are all included in a single exe, in my case Windows detects this as something dangerous im not sure why, but you are free to compile the project yourself using the other options.**
- Compile the project using the Visual Studio solution.
- Compile the code yourself (if you have any SFML library related issue, follow [this](https://www.sfml-dev.org/tutorials/2.5/) guide).

## License 📜
[MPL 2.0 License](https://www.mozilla.org/en-US/MPL/2.0/)
45 changes: 41 additions & 4 deletions SortAlgorithms.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,71 @@
#include "SortAlgorithms.h"

void algo::bubbleSort(std::vector<Sortable>& sortElements, int timeSleep) {
int algo::bubbleSort(std::vector<Sortable>& sortElements, int timeSleep) {
int numOfComparisons = 0;

for (int n = 0; n < sortElements.size() - 1; n++) {
if (sortElements[n].value > sortElements[n + 1].value) {
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[n+1]);
}
numOfComparisons++;
}

return numOfComparisons;
}

void algo::selectionSort(std::vector<Sortable>& sortElements, int timeSleep) {
int algo::selectionSort(std::vector<Sortable>& sortElements, int timeSleep) {
int numOfComparisons = 0;

for (int n = 0; n <= sortElements.size() - 1; n++) {
for (int j = n; j <= sortElements.size() - 1; j++) {
if (sortElements[n].value > sortElements[j].value) {
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[j]);

}
numOfComparisons++;
}
}

return numOfComparisons;
}

int algo::insertionSort(std::vector<Sortable>& sortElements, int timeSleep) {
int numOfComparisons = 0;

for (int n = 1; n < sortElements.size(); n++)
{
auto temp = sortElements[n];
int j = n - 1;
while (j >= 0 && temp.value <= sortElements[j].value)
{
sortElements[j].color = sf::Color::Red;

sortElements[j + 1] = sortElements[j];

sf::sleep(sf::milliseconds(timeSleep));
sortElements[j+1].color = sf::Color::White;

j = j - 1;

numOfComparisons++;

}
sortElements[j + 1] = temp;
numOfComparisons++;
}

return numOfComparisons;
}

void algoUtils::swap(std::vector<Sortable>& sortElements, int timeSleep, Sortable& el1, Sortable& el2) {
el1.color = sf::Color::Red;
el2.color = sf::Color::Red;

// Swap positions
auto currElement = el1;
auto tempElement = el2;
el1 = tempElement;
el2 = currElement;

// Wait timeSleep ms between iterations
sf::sleep(sf::milliseconds(timeSleep));

el1.color = sf::Color::White;
Expand Down
5 changes: 3 additions & 2 deletions SortAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include "Sortable.h"

namespace algo {
void bubbleSort(std::vector<Sortable>& sortElements, int timeSleep);
void selectionSort(std::vector<Sortable>& sortElements, int timeSleep);
int bubbleSort(std::vector<Sortable>& sortElements, int timeSleep);
int selectionSort(std::vector<Sortable>& sortElements, int timeSleep);
int insertionSort(std::vector<Sortable>& sortElements, int timeSleep);
}

namespace algoUtils {
Expand Down
45 changes: 33 additions & 12 deletions SortController.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "SortController.h"
#include <iostream>
#include <SFML/System.hpp>
#include "Utils.h"

SortController::SortController(sf::Vector2u windowSize, int timeSleep) {
this->winWidth = windowSize.x;
Expand All @@ -21,7 +20,7 @@ void SortController::clear() {

void SortController::populate(int numOfElements) {
for (int n = 0; n < numOfElements; n++) {
Sortable sortable(((float)winWidth / numOfElements), ((float)winHeight / numOfElements) * (n+1), n); // Width defined to space max space in window, height defined by Sortable value
Sortable sortable(((float)winWidth / numOfElements), ((float)winHeight / numOfElements) * (n+1), n); // Width defined for max space in window, height defined by element value
sortElements.push_back(sortable);
}
}
Expand All @@ -36,6 +35,22 @@ void SortController::setTimeSleep(int t) {
timeSleep = t;
}

void SortController::displaySortInfo(int sortType, bool isSorting, int numOfComparisons, int sortTime) {
system(CLEAR);

std::cout << "Sort type: " << Utils::getSortType(sortType) << std::endl << std::endl;

if (isSorting) {
std::cout << std::endl << "Sorting..." << std::endl;
}
else {
std::cout << "Sort time: " << sortTime << "ms || " << (double)sortTime/1000 << "s" << std::endl;
std::cout << "Number of elements: " << sortElements.size() << std::endl;
std::cout << "Time between comparisons: " << timeSleep << "ms" << std::endl;
std::cout << "Number of comparisons: " << numOfComparisons << std::endl;
}
}

///////////////////////////////
//
// Sorting methods
Expand All @@ -44,26 +59,32 @@ void SortController::setTimeSleep(int t) {

void SortController::startSort(int sortType) {
isSorting = true;
sf::Clock timeSort;
int numOfComparisons = 0;
sf::Clock sortTime;

displaySortInfo(sortType, isSorting, numOfComparisons, sortTime.getElapsedTime().asMilliseconds());

while (!isSorted())
{

switch (sortType)
{
case 0:
algo::bubbleSort(sortElements, timeSleep);
numOfComparisons += algo::bubbleSort(sortElements, timeSleep);
break;
case 1:
algo::selectionSort(sortElements, timeSleep);
numOfComparisons += algo::selectionSort(sortElements, timeSleep);
break;
default:
case 2:
numOfComparisons += algo::insertionSort(sortElements, timeSleep);
break;
default:
return;
}
}

std::cout << timeSort.getElapsedTime().asMilliseconds() << "ms" << std::endl;
checkSort();
isSorting = false;
displaySortInfo(sortType, isSorting, numOfComparisons, sortTime.getElapsedTime().asMilliseconds());
checkSortAnim();
}

bool SortController::isSorted() {
Expand All @@ -77,8 +98,8 @@ bool SortController::isSorted() {
return true;
};

// This function is only for the "checking animation", at this point, the vector is 100% sorted, verified by isSorted()
void SortController::checkSort() {
// This function is only for the "checking animation", the verification is made by isSorted()
void SortController::checkSortAnim() {
for (int n = 0; n < sortElements.size(); n++) {
sortElements[n].color = sf::Color::Green;
sf::sleep(sf::milliseconds(timeSleep));
Expand Down
10 changes: 9 additions & 1 deletion SortController.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#pragma once
#ifdef _WIN32
#define CLEAR "cls"
#else
#define CLEAR "clear"
#endif

#include <vector>
#include <algorithm>
#include <random>
#include <iostream>
#include <SFML/System.hpp>

#include "Sortable.h"
Expand All @@ -23,10 +30,11 @@ class SortController
void populate(int numOfElements);
void randomize();
void setTimeSleep(int t);
void displaySortInfo(int sortType, bool isSorting, int numOfComparisons, int sortTime);

// Sorting methods
void startSort(int sortType);
void checkSort();
void checkSortAnim();
bool isSorted();
};

18 changes: 18 additions & 0 deletions Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "Utils.h"

std::string Utils::getSortType(int sortType) {
switch (sortType)
{
case 0:
return "Bubble sort";

case 1:
return "Selection sort";

case 2:
return "Insertion sort";

default:
return std::to_string(sortType);
}
}
8 changes: 8 additions & 0 deletions Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <string>

namespace Utils
{
std::string getSortType(int sortType);
};

Loading

0 comments on commit e012576

Please sign in to comment.