-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionSort.cpp
89 lines (73 loc) · 2.27 KB
/
InsertionSort.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
//
// Created by daily on 04-01-24.
//
#include "InsertionSort.h"
#include "Constants.hpp"
#include <cstdlib>
#include <ctime>
#include <iostream>
InsertionSort::InsertionSort(int dataSize) :
data(),
currentIndex(1),
keyIndex(0),
sorting(false),
dataSize_(dataSize),
screenWidth(0),
screenHeight(0),
isComplete(false)
{
srand(static_cast<unsigned>(time(nullptr)));
for(int i = 0; i < dataSize; i++){
int val = (rand() % 99) + 1;
data.push_back(val);
}
}
void InsertionSort::update() {
if (isComplete) return;
if (!sorting) {
sorting = true;
return;
}
if (currentIndex < dataSize_) {
int key = data[static_cast<size_t>(currentIndex)];
keyIndex = currentIndex - 1;
while (keyIndex >= 0 && data[static_cast<unsigned long>(keyIndex)] > key) {
data[static_cast<size_t>(keyIndex + 1)] = data[static_cast<size_t>(keyIndex)];
keyIndex--;
}
data[static_cast<size_t>(keyIndex + 1)] = key;
currentIndex++;
} else {
isComplete = true;
sorting = false;
}
}
void InsertionSort::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<size_t>(i)] * (screenHeight / 100);
int barTop = screenHeight - barHeight;
// Change color based on the state
if (i == currentIndex) {
SDL_SetRenderDrawColor(renderer, 255, 165, 0, 255); // Orange for the current index
} else if (i <= currentIndex) {
SDL_SetRenderDrawColor(renderer, 124, 252, 0, 255); // Lawn green for sorted part
} else {
SDL_SetRenderDrawColor(renderer, 173, 216, 230, 255); // Light blue for unsorted part
}
SDL_Rect rect = {i * (barWidth + 2), barTop, barWidth, barHeight};
SDL_RenderFillRect(renderer, &rect);
}
// Additional rendering for text information can go here
SDL_RenderPresent(renderer);
}
void InsertionSort::startSort() {
sorting = true;
currentIndex = 1;
}
void InsertionSort::setScreenDimensions(int width, int height) {
screenWidth = width;
screenHeight = height;
}