Skip to content

Commit 09df3a3

Browse files
committed
Added color for moving elements and checking animation at the end. Added more key events: Change time in milliseconds between each comparison, change number of elements and stop sorting.
1 parent 20e2102 commit 09df3a3

File tree

6 files changed

+99
-54
lines changed

6 files changed

+99
-54
lines changed

SortAlgorithms.cpp

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
11
#include "SortAlgorithms.h"
2-
#include <iostream>
3-
#include <chrono>
4-
#include <thread>
52

6-
namespace algo {
7-
void bubbleSort(std::vector<Sortable>& sortElements, int timeSleep) {
3+
void algo::bubbleSort(std::vector<Sortable>& sortElements, int timeSleep) {
4+
for (int n = 0; n < sortElements.size() - 1; n++) {
5+
if (sortElements[n].value > sortElements[n + 1].value) {
6+
sortElements[n+1].color = sf::Color::Red;
7+
sortElements[n].color = sf::Color::Red;
88

9-
for (int n = 0; n < sortElements.size() - 1; n++) {
10-
11-
/*
12-
std::cout << "[";
13-
for (auto i : sortElements) {
14-
std::cout << i.value << ", ";
15-
}
16-
std::cout << "]" << std::endl;
17-
18-
*/
19-
20-
if (sortElements[n].value > sortElements[n + 1].value) {
21-
// Change color while sorting
22-
sortElements[n].color = sf::Color::Red;
23-
sortElements[n+1].color = sf::Color::Red;
24-
25-
// Swap positions
26-
auto currElement = sortElements[n];
27-
auto tempElement = sortElements[n + 1];
28-
sortElements[n + 1] = currElement;
29-
sortElements[n] = tempElement;
30-
31-
// Reset color
32-
sortElements[n].color = sf::Color::White;
33-
sortElements[n + 1].color = sf::Color::White;
34-
}
9+
// Swap positions
10+
auto currElement = sortElements[n];
11+
auto tempElement = sortElements[n + 1];
12+
sortElements[n + 1] = currElement;
13+
sortElements[n] = tempElement;
3514

3615
// Wait timeSleep ms between iterations
37-
std::this_thread::sleep_for(std::chrono::milliseconds(timeSleep));
16+
sf::sleep(sf::milliseconds(timeSleep));
17+
18+
sortElements[n+1].color = sf::Color::White;
19+
sortElements[n].color = sf::Color::White;
3820
}
3921
}
4022
}

SortAlgorithms.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#pragma once
22
#include <vector>
3+
4+
#if defined(_WIN32)
5+
#include <Windows.h>
6+
#define PLATFORM "windows"
7+
#endif
8+
39
#include "Sortable.h"
410

511
namespace algo {

SortController.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "SortController.h"
2+
#include <iostream>
3+
#include <SFML/System.hpp>
24

35
SortController::SortController(sf::Vector2u windowSize, int timeSleep) {
46
this->winWidth = windowSize.x;
@@ -7,38 +9,54 @@ SortController::SortController(sf::Vector2u windowSize, int timeSleep) {
79
this->timeSleep = timeSleep;
810
}
911

12+
///////////////////////////////
13+
//
14+
// Vector control methods
15+
//
16+
///////////////////////////////
17+
1018
void SortController::clear() {
1119
sortElements.clear();
1220
}
1321

1422
void SortController::populate(int numOfElements) {
1523
for (int n = 0; n < numOfElements; n++) {
16-
std::cout << (winWidth / numOfElements) << std::endl;
1724
Sortable sortable(((float)winWidth / numOfElements), ((float)winHeight / numOfElements) * (n+1), n); // Width defined to space max space in window, height defined by Sortable value
1825
sortElements.push_back(sortable);
1926
}
2027
}
2128

29+
void SortController::randomize() {
30+
auto rd = std::random_device{};
31+
auto rng = std::default_random_engine{ rd() };
32+
std::shuffle(std::begin(sortElements), std::end(sortElements), rng);
33+
};
34+
35+
void SortController::setTimeSleep(int t) {
36+
timeSleep = t;
37+
}
38+
39+
///////////////////////////////
40+
//
41+
// Sorting methods
42+
//
43+
///////////////////////////////
44+
2245
void SortController::startSort(int sortType) {
23-
std::cout << "Sort started via thread!" << std::endl;
2446
isSorting = true;
47+
sf::Clock timeSort;
2548
while (!isSorted())
2649
{
2750
if (sortType == 0) {
2851
algo::bubbleSort(sortElements, timeSleep);
2952
}
3053
}
54+
55+
std::cout << timeSort.getElapsedTime().asMilliseconds() << std::endl;
56+
checkSort();
3157
isSorting = false;
32-
std::cout << "Sorting finished!" << std::endl;
33-
std::cout << std::endl;
3458
}
3559

36-
void SortController::randomize() {
37-
auto rd = std::random_device{};
38-
auto rng = std::default_random_engine{ rd() };
39-
std::shuffle(std::begin(sortElements), std::end(sortElements), rng);
40-
};
41-
4260
bool SortController::isSorted() {
4361
for (int n = 0; n < sortElements.size()-1; n++) {
4462
if (sortElements[n].value > sortElements[n+1].value)
@@ -50,6 +68,10 @@ bool SortController::isSorted() {
5068
return true;
5169
};
5270

71+
// This function is only for the "checking animation", at this point, the vector is 100% sorted, verified by isSorted()
5372
void SortController::checkSort() {
54-
73+
for (int n = 0; n < sortElements.size(); n++) {
74+
sortElements[n].color = sf::Color::Green;
75+
sf::sleep(sf::milliseconds(timeSleep));
76+
}
5577
};

SortController.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <vector>
33
#include <algorithm>
44
#include <random>
5+
#include <SFML/System.hpp>
6+
57
#include "Sortable.h"
68
#include "SortAlgorithms.h"
79

@@ -15,11 +17,16 @@ class SortController
1517
std::vector<Sortable> sortElements;
1618

1719
SortController(sf::Vector2u windowSize, int timeSleep);
20+
21+
// Vector control methods
1822
void clear();
1923
void populate(int numOfElements);
20-
void startSort(int sortType);
2124
void randomize();
22-
bool isSorted();
25+
void setTimeSleep(int t);
26+
27+
// Sorting methods
28+
void startSort(int sortType);
2329
void checkSort();
30+
bool isSorted();
2431
};
2532

Sortable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class Sortable
66
{
77
public:
8-
sf::Color color = sf::Color::White;
8+
sf::Color color = sf::Color::White; // white 42...95, red: 42...35
99
int value;
1010
float width, height;
1111

main.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22
- Sort Types -
33
0: Bubble Sort
44
1: ...
5+
6+
Due to the 1ms resolution of timers on Windows, the speed scale is smaller. The Linux version has a higher time resoltion < 1ms!
57
*/
68

79
#include <SFML/Graphics.hpp>
810
#include <iostream>
911
#include <thread>
1012
#include "SortController.h"
1113

12-
void test(int num) {
13-
std::cout << "Hi, this is a test and this is my number: " << num << std::endl;
14-
}
15-
1614
int main()
1715
{
18-
sf::RenderWindow window(sf::VideoMode(600, 400), "Sorting visualizer v0.1-alpha");
16+
sf::RenderWindow window(sf::VideoMode(600, 400), "Sorting visualizer v0.2-alpha");
1917

20-
int numOfElements = 10;
21-
int timeSleep = 1;
18+
int numOfElements = 50;
19+
int timeSleep = 1; // milliseconds
2220
int sortType = 0;
2321

2422
std::thread sortingThread;
@@ -39,13 +37,23 @@ int main()
3937
// Randomize and start sorting
4038
case sf::Keyboard::Space:
4139
if (!sortController.isSorting) {
42-
std::cout << "Randomizing..." << std::endl;
40+
std::cout << "Starting sort!" << std::endl;
41+
sortController.clear();
42+
sortController.populate(numOfElements);
4343
sortController.randomize();
4444

4545
sortingThread = std::thread(&SortController::startSort, &sortController, sortType);
4646
sortingThread.detach();
4747
}
48+
break;
4849

50+
// Stop sort
51+
case sf::Keyboard::Backspace:
52+
if (sortController.isSorting) {
53+
std::cout << "Sort stopped!" << std::endl;
54+
sortController.clear();
55+
sortController.populate(numOfElements);
56+
}
4957
break;
5058

5159
// Change sort type (increase)
@@ -76,6 +84,25 @@ int main()
7684
std::cout << "Num of elements changed to: " << numOfElements << std::endl;
7785
break;
7886

87+
// Change number of sortables through console
88+
case sf::Keyboard::F1:
89+
std::cout << "Number of sortables: ";
90+
std::cin >> numOfElements;
91+
std::cout << std::endl;
92+
93+
sortController.clear();
94+
sortController.populate(numOfElements);
95+
break;
96+
97+
// Change time between iterations
98+
case sf::Keyboard::F2:
99+
std::cout << "Time between iterations (milliseconds): ";
100+
std::cin >> timeSleep;
101+
std::cout << std::endl;
102+
103+
sortController.setTimeSleep(timeSleep);
104+
break;
105+
79106
default:
80107
break;
81108
}
@@ -91,6 +118,7 @@ int main()
91118
int index = 0;
92119
for (auto sortable : sortController.sortElements) {
93120
sf::RectangleShape shape = sortable.shape();
121+
shape.setFillColor(sortable.color);
94122
shape.setPosition(sf::Vector2f(sortable.width * index++, sortController.winHeight - sortable.height));
95123
window.draw(shape);
96124
}

0 commit comments

Comments
 (0)