Skip to content

Commit e012576

Browse files
committed
v1.0 final
1 parent db0de6c commit e012576

9 files changed

+194
-57
lines changed

Diff for: 05-sorting-visualizer.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,13 @@
159159
<ClCompile Include="Sortable.cpp" />
160160
<ClCompile Include="SortAlgorithms.cpp" />
161161
<ClCompile Include="SortController.cpp" />
162+
<ClCompile Include="Utils.cpp" />
162163
</ItemGroup>
163164
<ItemGroup>
164165
<ClInclude Include="Sortable.h" />
165166
<ClInclude Include="SortAlgorithms.h" />
166167
<ClInclude Include="SortController.h" />
168+
<ClInclude Include="Utils.h" />
167169
</ItemGroup>
168170
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
169171
<ImportGroup Label="ExtensionTargets">

Diff for: README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Sorting Visualizer 📚
2+
3+
A lightweight sorting visualizer made with C++ and [SFML](https://www.sfml-dev.org/index.php).
4+
5+
![Bubble sort](https://i.imgur.com/iFIRllR.png) <br>
6+
*Bubble sort*
7+
8+
![Bubble sort info](https://i.imgur.com/lZL6G0a.png) <br>
9+
*Sort info*
10+
11+
## Sort types 🗂️
12+
- Bubble sort
13+
- Selection sort
14+
- Insertion sort
15+
16+
## Usage 🕹️
17+
**F1**: Change number of elements <br>
18+
**F2**: Change time between comparisons <br>
19+
**Arrow Up / Arrow down**: Change sort type <br>
20+
**Space**: Start sort <br>
21+
**Backspace**: Stop sort
22+
23+
## Known issues ❗
24+
- Program stops when a non existing sort type tries to be loaded
25+
26+
## Download 🖨️
27+
- Download pre-compiled exe from [Releases](https://github.com/alesbe/sorting-visualizer/releases). <br>
28+
**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.**
29+
- Compile the project using the Visual Studio solution.
30+
- Compile the code yourself (if you have any SFML library related issue, follow [this](https://www.sfml-dev.org/tutorials/2.5/) guide).
31+
32+
## License 📜
33+
[MPL 2.0 License](https://www.mozilla.org/en-US/MPL/2.0/)

Diff for: SortAlgorithms.cpp

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,71 @@
11
#include "SortAlgorithms.h"
22

3-
void algo::bubbleSort(std::vector<Sortable>& sortElements, int timeSleep) {
3+
int algo::bubbleSort(std::vector<Sortable>& sortElements, int timeSleep) {
4+
int numOfComparisons = 0;
5+
46
for (int n = 0; n < sortElements.size() - 1; n++) {
57
if (sortElements[n].value > sortElements[n + 1].value) {
68
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[n+1]);
79
}
10+
numOfComparisons++;
811
}
12+
13+
return numOfComparisons;
914
}
1015

11-
void algo::selectionSort(std::vector<Sortable>& sortElements, int timeSleep) {
16+
int algo::selectionSort(std::vector<Sortable>& sortElements, int timeSleep) {
17+
int numOfComparisons = 0;
18+
1219
for (int n = 0; n <= sortElements.size() - 1; n++) {
1320
for (int j = n; j <= sortElements.size() - 1; j++) {
1421
if (sortElements[n].value > sortElements[j].value) {
1522
algoUtils::swap(sortElements, timeSleep, sortElements[n], sortElements[j]);
23+
1624
}
25+
numOfComparisons++;
1726
}
1827
}
28+
29+
return numOfComparisons;
30+
}
31+
32+
int algo::insertionSort(std::vector<Sortable>& sortElements, int timeSleep) {
33+
int numOfComparisons = 0;
34+
35+
for (int n = 1; n < sortElements.size(); n++)
36+
{
37+
auto temp = sortElements[n];
38+
int j = n - 1;
39+
while (j >= 0 && temp.value <= sortElements[j].value)
40+
{
41+
sortElements[j].color = sf::Color::Red;
42+
43+
sortElements[j + 1] = sortElements[j];
44+
45+
sf::sleep(sf::milliseconds(timeSleep));
46+
sortElements[j+1].color = sf::Color::White;
47+
48+
j = j - 1;
49+
50+
numOfComparisons++;
51+
52+
}
53+
sortElements[j + 1] = temp;
54+
numOfComparisons++;
55+
}
56+
57+
return numOfComparisons;
1958
}
2059

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

25-
// Swap positions
2664
auto currElement = el1;
2765
auto tempElement = el2;
2866
el1 = tempElement;
2967
el2 = currElement;
3068

31-
// Wait timeSleep ms between iterations
3269
sf::sleep(sf::milliseconds(timeSleep));
3370

3471
el1.color = sf::Color::White;

Diff for: SortAlgorithms.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
#include "Sortable.h"
44

55
namespace algo {
6-
void bubbleSort(std::vector<Sortable>& sortElements, int timeSleep);
7-
void selectionSort(std::vector<Sortable>& sortElements, int timeSleep);
6+
int bubbleSort(std::vector<Sortable>& sortElements, int timeSleep);
7+
int selectionSort(std::vector<Sortable>& sortElements, int timeSleep);
8+
int insertionSort(std::vector<Sortable>& sortElements, int timeSleep);
89
}
910

1011
namespace algoUtils {

Diff for: SortController.cpp

+33-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "SortController.h"
2-
#include <iostream>
3-
#include <SFML/System.hpp>
2+
#include "Utils.h"
43

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

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

38+
void SortController::displaySortInfo(int sortType, bool isSorting, int numOfComparisons, int sortTime) {
39+
system(CLEAR);
40+
41+
std::cout << "Sort type: " << Utils::getSortType(sortType) << std::endl << std::endl;
42+
43+
if (isSorting) {
44+
std::cout << std::endl << "Sorting..." << std::endl;
45+
}
46+
else {
47+
std::cout << "Sort time: " << sortTime << "ms || " << (double)sortTime/1000 << "s" << std::endl;
48+
std::cout << "Number of elements: " << sortElements.size() << std::endl;
49+
std::cout << "Time between comparisons: " << timeSleep << "ms" << std::endl;
50+
std::cout << "Number of comparisons: " << numOfComparisons << std::endl;
51+
}
52+
}
53+
3954
///////////////////////////////
4055
//
4156
// Sorting methods
@@ -44,26 +59,32 @@ void SortController::setTimeSleep(int t) {
4459

4560
void SortController::startSort(int sortType) {
4661
isSorting = true;
47-
sf::Clock timeSort;
62+
int numOfComparisons = 0;
63+
sf::Clock sortTime;
64+
65+
displaySortInfo(sortType, isSorting, numOfComparisons, sortTime.getElapsedTime().asMilliseconds());
66+
4867
while (!isSorted())
4968
{
50-
5169
switch (sortType)
5270
{
5371
case 0:
54-
algo::bubbleSort(sortElements, timeSleep);
72+
numOfComparisons += algo::bubbleSort(sortElements, timeSleep);
5573
break;
5674
case 1:
57-
algo::selectionSort(sortElements, timeSleep);
75+
numOfComparisons += algo::selectionSort(sortElements, timeSleep);
5876
break;
59-
default:
77+
case 2:
78+
numOfComparisons += algo::insertionSort(sortElements, timeSleep);
6079
break;
80+
default:
81+
return;
6182
}
6283
}
6384

64-
std::cout << timeSort.getElapsedTime().asMilliseconds() << "ms" << std::endl;
65-
checkSort();
6685
isSorting = false;
86+
displaySortInfo(sortType, isSorting, numOfComparisons, sortTime.getElapsedTime().asMilliseconds());
87+
checkSortAnim();
6788
}
6889

6990
bool SortController::isSorted() {
@@ -77,8 +98,8 @@ bool SortController::isSorted() {
7798
return true;
7899
};
79100

80-
// This function is only for the "checking animation", at this point, the vector is 100% sorted, verified by isSorted()
81-
void SortController::checkSort() {
101+
// This function is only for the "checking animation", the verification is made by isSorted()
102+
void SortController::checkSortAnim() {
82103
for (int n = 0; n < sortElements.size(); n++) {
83104
sortElements[n].color = sf::Color::Green;
84105
sf::sleep(sf::milliseconds(timeSleep));

Diff for: SortController.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#pragma once
2+
#ifdef _WIN32
3+
#define CLEAR "cls"
4+
#else
5+
#define CLEAR "clear"
6+
#endif
7+
28
#include <vector>
39
#include <algorithm>
410
#include <random>
11+
#include <iostream>
512
#include <SFML/System.hpp>
613

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

2735
// Sorting methods
2836
void startSort(int sortType);
29-
void checkSort();
37+
void checkSortAnim();
3038
bool isSorted();
3139
};
3240

Diff for: Utils.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "Utils.h"
2+
3+
std::string Utils::getSortType(int sortType) {
4+
switch (sortType)
5+
{
6+
case 0:
7+
return "Bubble sort";
8+
9+
case 1:
10+
return "Selection sort";
11+
12+
case 2:
13+
return "Insertion sort";
14+
15+
default:
16+
return std::to_string(sortType);
17+
}
18+
}

Diff for: Utils.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
#include <string>
3+
4+
namespace Utils
5+
{
6+
std::string getSortType(int sortType);
7+
};
8+

0 commit comments

Comments
 (0)