-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.cpp
121 lines (119 loc) · 4.42 KB
/
BubbleSort.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Created by daily on 03-01-24.
//
#include "BubbleSort.h"
#include "Constants.hpp"
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
BubbleSort::BubbleSort(int dataSize) :
data(),
currentIdx(0),
sorting(false),
dataSize_(dataSize),
screenWidth(0),
screenHeight(0),
comparisonIdx(-1),
swappedInLastPass(false),
sortedCount(0),
lastSwapIndex(dataSize-1),
swapCount(0),
comparisonCount(0),
greenIterationStarted(false),
whiteIterationStarted(false),
iterationCount(0),
isComplete(false)
{
srand(static_cast<unsigned>(time(nullptr)));
// std::cout << "Constructor: dataSize_ = " << dataSize_ << ", screenWidth = " << screenWidth << ", screenHeight = " << screenHeight << std::endl;
for(int i = 0; i < dataSize; i++){
int val = (rand() % 99) + 1;
data.push_back(val);
// std::cout << "Data[" << i << "] = " << val << std::endl;
}
}
void BubbleSort::update() {
if(isComplete) return;
if (!sorting && !greenIterationStarted && !whiteIterationStarted) {
greenIterationStarted = true;
}
if (greenIterationStarted) {
iterationCount++;
if (iterationCount > 1) {
greenIterationStarted = false;
whiteIterationStarted = true;
iterationCount = 0;
return;
}
}
if (whiteIterationStarted) {
iterationCount++;
if (iterationCount > 1) {
whiteIterationStarted = false;
iterationCount = 0;
isComplete = true;
return;
}
}
if (sorting) {
if (currentIdx < dataSize_ - sortedCount - 1) {
comparisonIdx = currentIdx;
comparisonCount++;
if (data[static_cast<size_t>(currentIdx + 1)] < data[static_cast<size_t>(currentIdx)]) {
std::swap(data[static_cast<size_t>(currentIdx + 1)], data[static_cast<size_t>(currentIdx)]);
swappedInLastPass = true;
lastSwapIndex = currentIdx;
swapCount++;
}
currentIdx++;
} else {
if (swappedInLastPass) {
swappedInLastPass = false;
currentIdx = 0;
sortedCount++;
} else {
sorting = false;
comparisonIdx = -1;
}
}
}
}
void BubbleSort::render(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
int barWidth = (screenWidth * 2) / (dataSize_ * 2) - 2;
for (int i = 0; i < dataSize_; ++i) {
int barHeight = data[static_cast<unsigned long>(i)] * (screenHeight / 100);
int barTop = screenHeight - barHeight;
if(isComplete){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
}
else if (greenIterationStarted && !whiteIterationStarted) {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} else if (whiteIterationStarted && !greenIterationStarted) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
} else {
if (i == comparisonIdx || i == comparisonIdx + 1) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
} else if (i >= dataSize_ - sortedCount) {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} else {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
}
}
SDL_Rect rect = {i * (barWidth + 2), barTop, barWidth, barHeight};
SDL_RenderFillRect(renderer, &rect);
}
SDL_RenderPresent(renderer);
}
void BubbleSort::startSort() {
sorting = true;
currentIdx = 0;
// std::cout << "Started sorting." << std::endl;
}
void BubbleSort::setScreenDimensions(int width, int height){
screenWidth = width;
screenHeight = height;
// std::cout << "Set Screen Dimensions: screenWidth = " << screenWidth << ", screenHeight = " << screenHeight << std::endl;
}