diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bbd11e2..dd468b8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,8 @@ add_executable(sortalgs "visualizers/histogram.c" "algorithms/bubblesort.c" "algorithms/mergesort.c" + "algorithms/insertionsort.c" + "algorithms/shellsort.c" ) target_include_directories(sortalgs PRIVATE diff --git a/src/algorithms.h b/src/algorithms.h index b8bfdb5..3dc0353 100644 --- a/src/algorithms.h +++ b/src/algorithms.h @@ -5,5 +5,7 @@ typedef struct Application Application; void bubblesort(Application*); void mergesort(Application*); +void insertionsort(Application*); +void shellsort(Application*); #endif \ No newline at end of file diff --git a/src/algorithms/bubblesort.c b/src/algorithms/bubblesort.c index 75213b6..148ad4a 100644 --- a/src/algorithms/bubblesort.c +++ b/src/algorithms/bubblesort.c @@ -1,4 +1,3 @@ -#include "algorithms.h" #include "application.h" void bubblesort(Application* app) diff --git a/src/algorithms/insertionsort.c b/src/algorithms/insertionsort.c new file mode 100644 index 0000000..c8b9654 --- /dev/null +++ b/src/algorithms/insertionsort.c @@ -0,0 +1,24 @@ +#include "application.h" + +void insertionsort(Application* app) +{ + for(int i = 1; i < visualizer_data.array_size; i++) + { + int new = visualizer_data.array[i]; + int k = i; + + while(k > 1 && visualizer_data.array[k - 1] > new) + { + visualizer_data.array[k] = visualizer_data.array[k - 1]; + k--; + + if(update_screen(app)) + return; + } + + visualizer_data.array[k] = new; + + if(update_screen(app)) + return; + } +} \ No newline at end of file diff --git a/src/algorithms/mergesort.c b/src/algorithms/mergesort.c index c7f95eb..8d2cfd9 100644 --- a/src/algorithms/mergesort.c +++ b/src/algorithms/mergesort.c @@ -1,4 +1,3 @@ -#include "algorithms.h" #include "application.h" #include diff --git a/src/algorithms/shellsort.c b/src/algorithms/shellsort.c new file mode 100644 index 0000000..1cfedb2 --- /dev/null +++ b/src/algorithms/shellsort.c @@ -0,0 +1,34 @@ +#include "application.h" + +void shellsort(Application* app) +{ + int step_size = 1; + while (3 * step_size + 1 < visualizer_data.array_size) + { + step_size = 3 * step_size + 1; + while (step_size > 0) + { + for(int i = step_size; i < visualizer_data.array_size; i++) + { + int new = visualizer_data.array[i]; + int k = i; + + while(k > step_size && visualizer_data.array[k - step_size] > new) + { + visualizer_data.array[k] = visualizer_data.array[k - step_size]; + k -= step_size; + + if(update_screen(app)) + return; + } + + visualizer_data.array[k] = new; + + if(update_screen(app)) + return; + } + + step_size /= 3; + } + } +} \ No newline at end of file diff --git a/src/application.c b/src/application.c index e05de69..14ea619 100644 --- a/src/application.c +++ b/src/application.c @@ -79,7 +79,9 @@ void run_application(Application* app) GLFWwindow* window = app->window; // bubblesort(app); - mergesort(app, 0, visualizer_data.array_size - 1); + // mergesort(app); + // insertionsort(app); + shellsort(app); while(!glfwWindowShouldClose(window)) {