diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6142ac1..bbd11e2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,8 @@ add_executable(sortalgs "application.c" "visualizer.c" "visualizers/histogram.c" + "algorithms/bubblesort.c" + "algorithms/mergesort.c" ) target_include_directories(sortalgs PRIVATE diff --git a/src/algorithms.h b/src/algorithms.h new file mode 100644 index 0000000..b8bfdb5 --- /dev/null +++ b/src/algorithms.h @@ -0,0 +1,9 @@ +#ifndef _ALGORITHMS_H +#define _ALGORITHMS_H + +typedef struct Application Application; + +void bubblesort(Application*); +void mergesort(Application*); + +#endif \ No newline at end of file diff --git a/src/algorithms/bubblesort.c b/src/algorithms/bubblesort.c new file mode 100644 index 0000000..75213b6 --- /dev/null +++ b/src/algorithms/bubblesort.c @@ -0,0 +1,21 @@ +#include "algorithms.h" +#include "application.h" + +void bubblesort(Application* app) +{ + for(size_t n = visualizer_data.array_size; n > 1; n--) + { + for(int i = 0; i < n - 1; i++) + { + if(visualizer_data.array[i] > visualizer_data.array[i + 1]) + { + int tmp = visualizer_data.array[i]; + visualizer_data.array[i] = visualizer_data.array[i + 1]; + visualizer_data.array[i + 1] = tmp; + } + + if(update_screen(app)) + return; + } + } +} \ No newline at end of file diff --git a/src/algorithms/mergesort.c b/src/algorithms/mergesort.c new file mode 100644 index 0000000..af3a659 --- /dev/null +++ b/src/algorithms/mergesort.c @@ -0,0 +1,67 @@ +#include "algorithms.h" +#include "application.h" + +#include + +static void merge(Application* app, size_t left, size_t middle, size_t right); +static void mergesort_rec(Application* app, size_t left, size_t right); + +static int* tmp = NULL; + +void mergesort(Application* app) +{ + tmp = (int*)malloc(visualizer_data.array_size * sizeof(int)); + mergesort_rec(app, 0, visualizer_data.array_size - 1); + free(tmp); +} + +static void mergesort_rec(Application* app, size_t left, size_t right) +{ + if(right > left) + { + size_t middle = (left + right) / 2; + mergesort_rec(app, left, middle); + mergesort_rec(app, middle + 1, right); + + merge(app, left, middle, right); + } +} + +static void merge(Application* app, size_t left, size_t middle, size_t right) +{ + for(size_t k = left; k <= middle; k++) + { + tmp[k] = visualizer_data.array[k]; + } + + for(size_t k = middle + 1; k <= right; k++) + { + tmp[right + middle + 1 - k] = visualizer_data.array[k]; + } + + size_t i = left; + size_t j = right; + size_t k = left; + + while(i < j) + { + if(tmp[i] <= tmp[j]) + { + visualizer_data.array[k] = tmp[i]; + i++; + } + else + { + visualizer_data.array[k] = tmp[j]; + j--; + } + + k++; + + if(update_screen(app)) + return; + } + + visualizer_data.array[right] = tmp[i]; + update_screen(app); +} \ No newline at end of file diff --git a/src/application.c b/src/application.c index 1113174..f23e06b 100644 --- a/src/application.c +++ b/src/application.c @@ -7,10 +7,7 @@ #include #include "visualizers/histogram.h" - -void bubblesort(Application* app); -void mergesort(Application* app, size_t left, size_t right); -void merge(Application* app, size_t left, size_t middle, size_t right); +#include "algorithms.h" void shuffle(int* array, size_t n); @@ -86,17 +83,23 @@ void run_application(Application* app) while(!glfwWindowShouldClose(window)) { - glfwPollEvents(); - - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - app->active_visualizer->render(); - - glfwSwapBuffers(window); + update_screen(app); } } +int update_screen(Application* app) +{ + glfwPollEvents(); + + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + app->active_visualizer->render(); + + glfwSwapBuffers(app->window); + return glfwWindowShouldClose(app->window); +} + void framebuffer_changed(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); @@ -115,104 +118,4 @@ void shuffle(int *array, size_t n) array[i] = t; } } -} - -void bubblesort(Application* app) -{ - for(size_t n = visualizer_data.array_size; n > 1; n--) - { - for(int i = 0; i < n - 1; i++) - { - if(visualizer_data.array[i] > visualizer_data.array[i + 1]) - { - int tmp = visualizer_data.array[i]; - visualizer_data.array[i] = visualizer_data.array[i + 1]; - visualizer_data.array[i + 1] = tmp; - } - - if(glfwWindowShouldClose(app->window)) - return; - - glfwPollEvents(); - - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - app->active_visualizer->render(); - - glfwSwapBuffers(app->window); - } - } -} - -void mergesort(Application* app, size_t left, size_t right) -{ - if(right > left) - { - size_t middle = (left + right) / 2; - mergesort(app, left, middle); - mergesort(app, middle + 1, right); - - merge(app, left, middle, right); - } -} - -void merge(Application* app, size_t left, size_t middle, size_t right) -{ - int* tmp = (int*)malloc(visualizer_data.array_size * sizeof(int)); - for(size_t k = left; k <= middle; k++) - { - tmp[k] = visualizer_data.array[k]; - } - - for(size_t k = middle + 1; k <= right; k++) - { - tmp[right + middle + 1 - k] = visualizer_data.array[k]; - } - - size_t i = left; - size_t j = right; - size_t k = left; - - while(i < j) - { - if(tmp[i] <= tmp[j]) - { - visualizer_data.array[k] = tmp[i]; - i++; - } - else - { - visualizer_data.array[k] = tmp[j]; - j--; - } - - k++; - - if(glfwWindowShouldClose(app->window)) - return; - - glfwPollEvents(); - - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - app->active_visualizer->render(); - - glfwSwapBuffers(app->window); - } - - visualizer_data.array[right] = tmp[i]; - - if(glfwWindowShouldClose(app->window)) - return; - - glfwPollEvents(); - - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - app->active_visualizer->render(); - - glfwSwapBuffers(app->window); } \ No newline at end of file diff --git a/src/application.h b/src/application.h index e43b52d..3e8ebc2 100644 --- a/src/application.h +++ b/src/application.h @@ -5,7 +5,7 @@ typedef struct GLFWwindow GLFWwindow; -typedef struct +typedef struct Application { GLFWwindow* window; Visualizer* active_visualizer; @@ -15,5 +15,6 @@ int init_application(Application* app, int width, int height, const char* title) void destroy_application(Application* app); void run_application(Application* app); +int update_screen(Application* app); #endif \ No newline at end of file