-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sort.cpp
65 lines (53 loc) · 1.04 KB
/
Sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "Sort.h"
Sort::Sort(sf::RenderWindow* window, std::stack<std::unique_ptr<State>>* stack_of_states,
std::vector<int>& array, CreateRectangle& rectBar, int speed)
: State(window, stack_of_states)
{
this->array = array;
this->rectBar = rectBar;
std::thread t(&Sort::bubbleSort, this);
t.detach();
}
void Sort::update()
{
}
void Sort::render()
{
rectBar.renderRectangle(window);
}
void Sort::initArray()
{
std::random_device rd;
std::default_random_engine engine{rd()};
std::uniform_int_distribution<int> ints{1, 50};
for (int i = 0; i < 50; i++)
{
array.emplace_back(ints(engine));
}
}
void Sort::bubbleSort()
{
int n = array.size();
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < n - i - 1; ++j)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (array[j] > array[j + 1])
{
std::swap(array[j], array[j + 1]);
rectBar.updateRect(0, j, j+1, array);
}
}
}
//this->stack_of_states->pop();
}
void Sort::initButtons()
{
}
void Sort::updateButtons()
{
}
void Sort::renderButtons()
{
}