Mercurial > public > algo-animator
comparison main.c @ 12:9ba546527bc2
fixing algo speed
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sat, 24 Jun 2023 00:44:30 +0100 |
parents | aaecc9b7ca9c |
children | 074bde2db09a |
comparison
equal
deleted
inserted
replaced
11:aaecc9b7ca9c | 12:9ba546527bc2 |
---|---|
1 #include <stdio.h> | 1 #include <stdio.h> |
2 #include <stdlib.h> | 2 #include <stdlib.h> |
3 #include <GL/glut.h> | 3 #include <GL/glut.h> |
4 #include <math.h> | 4 #include <math.h> |
5 #include <time.h> | 5 #include <time.h> |
6 #include <stdbool.h> | |
6 | 7 |
7 | 8 |
8 #define WINDOW_HEIGHT 1080 | 9 #define WINDOW_HEIGHT 1080 |
9 #define WINDOW_WIDTH 1920 | 10 #define WINDOW_WIDTH 1920 |
10 #define FPS 60 | |
11 #define OFFSET 150 | 11 #define OFFSET 150 |
12 #define RECT_WIDTH 5 | 12 #define RECT_WIDTH 5 |
13 #define SPACE 1 | 13 #define SPACE 1 |
14 | 14 |
15 | 15 |
16 // Globals | 16 // Globals |
17 char* algos[] = {"Bubble sort", "Selection sort", "Insertion sort", "Quick sort"}; | 17 char algos[4][50] = { |
18 "Bubble sort", | |
19 "Selection sort", | |
20 "Insertion sort", | |
21 "Quick sort" | |
22 }; | |
23 | |
18 int selected_algo = 0; | 24 int selected_algo = 0; |
19 int n_algos = sizeof(algos) / sizeof(algos[0]); | 25 int refresh_counter = 0; |
26 int iter_counter = 0; | |
27 int arr_size; | |
20 | 28 |
21 int* arr; | 29 int* arr; |
22 int arr_size; | 30 |
31 bool run; | |
23 | 32 |
24 | 33 |
25 // Algos | 34 // Algos |
26 int step = 0; | 35 |
27 int i = 0; | 36 struct BubbleSortInfo { |
37 int step; | |
38 int i; | |
39 }; | |
40 | |
41 struct BubbleSortInfo bs = {0, 0}; | |
42 | |
28 void bubble_sort() { | 43 void bubble_sort() { |
29 if (i < arr_size - step - 1) { | 44 if (bs.i < arr_size - bs.step - 1) { |
30 int current = arr[i]; | 45 int current = arr[bs.i]; |
31 int next = arr[i + 1]; | 46 int next = arr[bs.i + 1]; |
32 | 47 |
33 if (current > next) { | 48 if (current > next) { |
34 arr[i + 1] = current; | 49 arr[bs.i + 1] = current; |
35 arr[i] = next; | 50 arr[bs.i] = next; |
36 } | 51 } |
37 | 52 |
38 i++; | 53 bs.i++; |
39 } else { | 54 } else { |
40 step++; | 55 bs.step++; |
41 i = 0; | 56 bs.i = 0; |
42 } | 57 } |
43 } | 58 } |
44 | 59 |
45 | 60 |
46 void create_array() { | 61 void create_array() { |
47 arr_size = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; | 62 arr_size = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; |
48 arr = (int*)malloc(arr_size * sizeof(int)); | 63 arr = (int*)malloc(arr_size * sizeof(int)); |
49 | 64 |
50 // srand(time(NULL)); | 65 srand(time(NULL)); |
51 | 66 |
52 int min = OFFSET; | 67 int min = OFFSET; |
53 int max = WINDOW_HEIGHT - OFFSET; | 68 int max = WINDOW_HEIGHT - OFFSET; |
54 | 69 |
55 for (int i = 0; i < arr_size; i++) { | 70 for (int i = 0; i < arr_size; i++) { |
56 arr[i] = rand() % ((max - min) + 1) + min; | 71 arr[i] = rand() % ((max - min) + 1) + min; |
57 } | 72 } |
58 } | 73 } |
59 | 74 |
60 | 75 |
61 void print_array() { | 76 bool array_sorted() { |
62 for (int i = 0; i < arr_size; i++) { | 77 for (int i = 0; i < arr_size - 1; i++) { |
63 printf("%d ", arr[i]); | 78 if (arr[i] > arr[i + 1]) { |
64 } | 79 return false; |
80 } | |
81 } | |
82 | |
83 return true; | |
65 } | 84 } |
66 | 85 |
67 | 86 |
68 void algo_selector(int direction) { | 87 void algo_selector(int direction) { |
69 int selection = selected_algo + direction; | 88 int selection = selected_algo + direction; |
139 render_text(20, OFFSET - 50, text); | 158 render_text(20, OFFSET - 50, text); |
140 | 159 |
141 sprintf(text, "Number of elements: %i", arr_size); | 160 sprintf(text, "Number of elements: %i", arr_size); |
142 render_text(20, OFFSET - 75, text); | 161 render_text(20, OFFSET - 75, text); |
143 | 162 |
163 sprintf(text, "Iterations: %i", iter_counter); | |
164 render_text(20, OFFSET - 100, text); | |
165 | |
166 | |
144 render_text(WINDOW_WIDTH - 500, OFFSET - 50, "Press 'a' or 's' to select an algorithm"); | 167 render_text(WINDOW_WIDTH - 500, OFFSET - 50, "Press 'a' or 's' to select an algorithm"); |
145 render_text(WINDOW_WIDTH - 500, OFFSET - 75, "Press 'enter' to run the algorithm"); | 168 render_text(WINDOW_WIDTH - 500, OFFSET - 75, "Press 'enter' to run the algorithm"); |
146 render_text(WINDOW_WIDTH - 500, OFFSET - 100, "Press 'r' to reset array"); | 169 render_text(WINDOW_WIDTH - 500, OFFSET - 100, "Press 'r' to reset array"); |
147 | 170 |
148 glutSwapBuffers(); | 171 glutSwapBuffers(); |
149 } | 172 } |
150 | 173 |
151 | 174 |
152 void timer(int value) { | 175 void idle() { |
153 bubble_sort(); | 176 if (run) { |
154 glutPostRedisplay(); | 177 bubble_sort(); |
155 glutTimerFunc(1000 / 800, timer, 0); | 178 refresh_counter++; |
179 iter_counter++; | |
180 | |
181 if (refresh_counter == 90) { | |
182 glutPostRedisplay(); | |
183 refresh_counter = 0; | |
184 } | |
185 | |
186 } else { | |
187 glutPostRedisplay(); | |
188 } | |
189 | |
190 if (array_sorted()) { | |
191 run = false; | |
192 } | |
156 } | 193 } |
157 | 194 |
158 | 195 |
159 void keyboard(unsigned char key, int x, int y) { | 196 void keyboard(unsigned char key, int x, int y) { |
160 | 197 |
161 // s | 198 // s |
162 if (key == 115) { | 199 if (key == 115) { |
163 algo_selector(1); | 200 algo_selector(1); |
164 } | 201 } |
165 | 202 |
203 // a | |
204 if (key == 97) { | |
205 algo_selector(-1); | |
206 } | |
207 | |
166 // r | 208 // r |
167 if (key == 114) { | 209 if (key == 114) { |
168 create_array(); | 210 create_array(); |
169 } | 211 iter_counter = 0; |
170 | 212 refresh_counter = 0; |
171 // a | 213 run = false; |
172 if (key == 97) { | 214 bs = (struct BubbleSortInfo){0, 0}; |
173 algo_selector(-1); | |
174 } | 215 } |
175 | 216 |
176 // enter | 217 // enter |
177 if (key == 13) { | 218 if (key == 13) { |
178 printf("Before sorting: "); | 219 run = true; |
179 print_array(); | |
180 printf("\n\n"); | |
181 | |
182 bubble_sort(); | |
183 | |
184 printf("After sorting: "); | |
185 print_array(); | |
186 } | 220 } |
187 } | 221 } |
188 | 222 |
189 | 223 |
190 int main(int argc, char** argv) { | 224 int main(int argc, char** argv) { |
195 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); | 229 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
196 glutCreateWindow("OpenGL Window"); | 230 glutCreateWindow("OpenGL Window"); |
197 setup(); | 231 setup(); |
198 glutDisplayFunc(display); | 232 glutDisplayFunc(display); |
199 glutKeyboardFunc(keyboard); | 233 glutKeyboardFunc(keyboard); |
200 glutTimerFunc(0, timer, 0); | 234 glutIdleFunc(idle); |
201 glutMainLoop(); | 235 glutMainLoop(); |
202 | 236 |
203 free(arr); | 237 free(arr); |
204 | 238 |
205 return 0; | 239 return 0; |