-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
142 lines (110 loc) · 3.36 KB
/
main.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <stdbool.h>
#define BAR 219
#define LINE 179
#define SPACE ' '
int get_random(int, int);
void sort(int[], int);
int* copy(int[], int);
void visualize(int[], int, char);
int* generate_array(int, int);
void clear_screen();
void set_cursor_visibility(bool);
int g_numbers_len;
int main(void) {
srand(time(NULL));
int *numbers = generate_array(1, 15);
set_cursor_visibility(false);
for (int i = 0; i < g_numbers_len - 1; i++) {
for (int j = 0; j < g_numbers_len - 1 - i; j++) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j + 1];
numbers[j + 1] = numbers[j];
numbers[j] = temp;
visualize(numbers, g_numbers_len, LINE);
Sleep(100);
clear_screen();
}
}
}
visualize(numbers, g_numbers_len, LINE);
free(numbers);
set_cursor_visibility(true);
return 0;
}
int get_random(int lower, int upper) {
return (rand() % (upper - (lower - 1))) + lower; // (lower - 1) makes 'upper' inclusive
}
int* generate_array(int lower, int upper) {
g_numbers_len = (upper - lower) + 1;
int *array = malloc(sizeof(int) * g_numbers_len);
for (int i = 0; i < g_numbers_len; i++) {
array[i] = get_random(lower, upper);
}
return array;
}
int* copy(int array[], int array_len) {
int *new_array = malloc(sizeof(int) * array_len);
for (int i = 0; i < array_len; i++) {
new_array[i] = array[i];
}
return new_array;
}
void visualize(int array[], int array_len, char style) {
int *b = copy(array, array_len);
sort(b, array_len);
char canvas[array_len][array_len];
for (int i = 0; i < array_len; i++) {
for (int j = 0; j < array_len; j++) {
if (array[j] >= b[array_len - 1 - i]) {
canvas[i][j] = style;
printf("%c", canvas[i][j]);
} else {
canvas[i][j] = SPACE;
printf("%c", canvas[i][j]);
}
}
printf("\n");
}
free(b);
}
void sort(int array[], int array_len) {
for (int i = 0; i < array_len - 1; i++) {
for (int j = 0; j < array_len - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
}
void clear_screen() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD topLeft = {0,0};
if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
abort();
}
DWORD length = csbi.dwSize.X * csbi.dwSize.Y;
DWORD written;
// fill the console buffer with 'space'
FillConsoleOutputCharacter(hOut, TEXT(' '), length, topLeft, &written);
// reset attributes of every character to default like bgcolor, fg etc.
FillConsoleOutputAttribute(hOut, csbi.wAttributes, length, topLeft, &written);
SetConsoleCursorPosition(hOut, topLeft);
}
void set_cursor_visibility(bool visible) {
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
if (visible) {
info.bVisible = TRUE;
} else {
info.bVisible = FALSE;
}
SetConsoleCursorInfo(consoleHandle, &info);
}