add shellsort + insertion sort

This commit is contained in:
Lauchmelder 2022-06-13 19:12:17 +02:00
parent 3c135b2765
commit ab2bfd5797
7 changed files with 65 additions and 3 deletions

View file

@ -5,6 +5,8 @@ add_executable(sortalgs
"visualizers/histogram.c" "visualizers/histogram.c"
"algorithms/bubblesort.c" "algorithms/bubblesort.c"
"algorithms/mergesort.c" "algorithms/mergesort.c"
"algorithms/insertionsort.c"
"algorithms/shellsort.c"
) )
target_include_directories(sortalgs PRIVATE target_include_directories(sortalgs PRIVATE

View file

@ -5,5 +5,7 @@ typedef struct Application Application;
void bubblesort(Application*); void bubblesort(Application*);
void mergesort(Application*); void mergesort(Application*);
void insertionsort(Application*);
void shellsort(Application*);
#endif #endif

View file

@ -1,4 +1,3 @@
#include "algorithms.h"
#include "application.h" #include "application.h"
void bubblesort(Application* app) void bubblesort(Application* app)

View file

@ -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;
}
}

View file

@ -1,4 +1,3 @@
#include "algorithms.h"
#include "application.h" #include "application.h"
#include <stdlib.h> #include <stdlib.h>

View file

@ -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;
}
}
}

View file

@ -79,7 +79,9 @@ void run_application(Application* app)
GLFWwindow* window = app->window; GLFWwindow* window = app->window;
// bubblesort(app); // bubblesort(app);
mergesort(app, 0, visualizer_data.array_size - 1); // mergesort(app);
// insertionsort(app);
shellsort(app);
while(!glfwWindowShouldClose(window)) while(!glfwWindowShouldClose(window))
{ {