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
122
123
124
125
126
127
128
129
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <SDL.h>
#include <SDL_image.h>
#define ASCII_SPACE 32
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define SRC_FONT_WIDTH 7
#define SRC_FONT_HEIGHT 9
#define DST_FONT_WIDTH 28
#define DST_FONT_HEIGHT 32
#define ATLAS_WIDTH 18
#define DATASET_SIZE 800
#define STATS_SIZE 80
int main(int argc, char **argv) {
srand(time(0));
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
SDL_Window *win = SDL_CreateWindow(
"Sorting algorithms",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
0);
SDL_Renderer *ren = SDL_CreateRenderer(
win,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Texture *font_atlas = IMG_LoadTexture(ren, "./font.png");
int to_be_sorted[DATASET_SIZE] = {0};
for (int i = 0; i < DATASET_SIZE;i++) {
to_be_sorted[i] = i + 1;
}
for (int i = 0; i < DATASET_SIZE; i++) {
int tmp = to_be_sorted[i];
int j = rand() % DATASET_SIZE;
to_be_sorted[i] = to_be_sorted[j];
to_be_sorted[j] = tmp;
}
int sort_index = 0;
int sort_index_internal = 0;
int comparisons = 0;
int swaps = 0;
while (1) {
SDL_Event e;
if (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
break;
}
}
for (int i = 0; i < 10; i++) {
if (sort_index < DATASET_SIZE - 1) {
if (sort_index_internal >= 0
&& to_be_sorted[sort_index_internal] > to_be_sorted[sort_index_internal + 1]) {
int tmp = to_be_sorted[sort_index_internal + 1];
to_be_sorted[sort_index_internal + 1] = to_be_sorted[sort_index_internal];
to_be_sorted[sort_index_internal] = tmp;
sort_index_internal--;
comparisons++;
swaps++;
} else {
sort_index++;
sort_index_internal = sort_index;
comparisons++;
}
}
}
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
SDL_RenderClear(ren);
char stats[STATS_SIZE] = {0};
sprintf(stats, "Comparisons: %d\nSwaps: %d", comparisons, swaps);
int line = 0;
int col = 0;
for (int i = 0; i < STATS_SIZE; i++) {
if (stats[i] == '\n') {
line++;
col = 0;
} else {
col++;
}
SDL_Rect src = {
(stats[i] - ASCII_SPACE) % ATLAS_WIDTH * SRC_FONT_WIDTH,
(stats[i] - ASCII_SPACE) / ATLAS_WIDTH * SRC_FONT_HEIGHT,
SRC_FONT_WIDTH,
SRC_FONT_HEIGHT
};
SDL_Rect dst = {
col * DST_FONT_WIDTH,
line * DST_FONT_HEIGHT,
DST_FONT_WIDTH,
DST_FONT_HEIGHT
};
SDL_RenderCopy(ren, font_atlas, &src, &dst);
}
for (int i = 0; i < DATASET_SIZE; i++) {
SDL_Rect bar = {
i * WINDOW_WIDTH / DATASET_SIZE,
WINDOW_HEIGHT,
WINDOW_WIDTH / DATASET_SIZE,
-to_be_sorted[i] * (int) (0.8 * WINDOW_HEIGHT) / DATASET_SIZE
};
if (sort_index_internal == i - 1) {
SDL_SetRenderDrawColor(ren, 0, 255, 0, 255);
} else if (sort_index == i - 1) {
SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
} else {
SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
}
SDL_RenderFillRect(ren, &bar);
}
SDL_RenderPresent(ren);
}
SDL_DestroyWindow(win);
SDL_DestroyRenderer(ren);
IMG_Quit();
SDL_Quit();
return 0;
}
|