Mercurial > public > algo-animator
changeset 31:61104b22a25d
I think it is working now...
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 29 Jun 2023 18:05:22 +0100 |
parents | f945bcc3571f |
children | b1a605eb721a |
files | README.md src/algorithms.c src/main.c src/utils.c src/utils.h |
diffstat | 5 files changed, 18 insertions(+), 36 deletions(-) [+] |
line wrap: on
line diff
--- a/README.md Thu Jun 29 17:48:36 2023 +0100 +++ b/README.md Thu Jun 29 18:05:22 2023 +0100 @@ -1,10 +1,14 @@ # algo-animator -This project is inspired by - off course - the video by Timo Bingmann called [15 sorting algorithms in 6 minutes](https://www.youtube.com/watch?v=kPRA0W1kECg). +An interactive program to visualize sorting algorithms. + +This project is inspired by - off course - the video by Timo Bingmann called +[15 sorting algorithms in 6 minutes](https://www.youtube.com/watch?v=kPRA0W1kECg).   + ## Compile ```bash @@ -14,29 +18,3 @@ make ./algo_animator ``` - - -## To improve - -Since `GLUT` is single-thread, I cannot call `glutPostRedisplay()` within `while` or `for loops` to redraw the screen in every -step of the selected sorting algorithm. I guess the solution is multi-threading, so I can perform the sorting in the second thread -and post notifications to the main thread to redraw the screen. - -Since this is my first OpenGL project and just my second program in C, I am tired and I don't want to spend more time in this project, -but maybe in the future I'll do it. Right now there are just two algorithms supported. I've implemented them in a way that every function -call is a single step of the sorting. So, storing the "state" outside the function, I can use `idle()` as the loop of the sorting algorithm. - -Let's say we selected `bubble sort`. Once you press `ENTER`, the process will be like this: - -```c -run = true; -bubble_sort(); -glutPostRedisplay; -bubble_sort(); -glutPostRedisplay; -bubble_sort(); -glutPostRedisplay; - -// Continue until the array is sorted -run = false; -```
--- a/src/algorithms.c Thu Jun 29 17:48:36 2023 +0100 +++ b/src/algorithms.c Thu Jun 29 18:05:22 2023 +0100 @@ -42,12 +42,16 @@ for (int i = step + 1; i < args->arr_size; i++) { args->comparisons++; args->arr[i].current = true; + args->arr[min_idx].current = true; + + control_flow(args->delay, args->sequentially, &args->pause); + args->arr[i].current = false; + args->arr[min_idx].current = false; if (args->arr[i].value < args->arr[min_idx].value) { min_idx = i; } - control_flow(args->delay, args->sequentially, &args->pause); } swap_elements(min_idx, step, args->arr);
--- a/src/main.c Thu Jun 29 17:48:36 2023 +0100 +++ b/src/main.c Thu Jun 29 18:05:22 2023 +0100 @@ -131,12 +131,12 @@ // Bottom: Column 1 render_text(20, vpadding - 50, "Press a or s to select an algorithm."); - render_text(20, vpadding - 80, "Press u or d to modify speed."); - render_text(20, vpadding - 110, "Press r to randomize the array."); + render_text(20, vpadding - 80, "Press u or d to change the delay."); + render_text(20, vpadding - 110, "Press r to reset."); // Bottom: Column 2 - render_text(800, vpadding - 50, "Press enter to run the algorithm."); - render_text(800, vpadding - 80, "Press p to pause the algorithm."); + render_text(800, vpadding - 50, "Press enter to run or resume the algorithm."); + render_text(800, vpadding - 80, "Press p to pause."); render_text(800, vpadding - 110, "Press q to enable or disable sequential mode."); glutSwapBuffers(); @@ -167,12 +167,12 @@ // u: Increase speed if (key == 117) { - change_speed(&algo_args, 10); + change_delay(&algo_args, 10); } // d: reduce speed if (key == 100) { - change_speed(&algo_args, -10); + change_delay(&algo_args, -10); } // enter: Run program
--- a/src/utils.c Thu Jun 29 17:48:36 2023 +0100 +++ b/src/utils.c Thu Jun 29 18:05:22 2023 +0100 @@ -39,7 +39,7 @@ } -void change_speed(struct AlgoArgs *algo_args, int change) { +void change_delay(struct AlgoArgs *algo_args, int change) { int new_delay = algo_args->delay + change; if (new_delay) {
--- a/src/utils.h Thu Jun 29 17:48:36 2023 +0100 +++ b/src/utils.h Thu Jun 29 18:05:22 2023 +0100 @@ -43,7 +43,7 @@ void randomize_array(struct Element *arr, int arr_size); bool array_sorted(struct Element *arr, int arr_size); void algorithm_selector(struct Algo *algos, int algos_size, int direction, int *selected_algo); -void change_speed(struct AlgoArgs *algo_args, int change); +void change_delay(struct AlgoArgs *algo_args, int change); void control_flow(useconds_t delay, bool sequentially, bool *pause); void reset_state(struct AlgoArgs *algo_args, struct ThreadState *thread_state); void run(struct AlgoArgs *algo_args, struct Algo *algos, int selected_algo,