fixed bug in mergesort exit condition

This commit is contained in:
Lauchmelder 2022-06-13 01:26:02 +02:00
parent 2aad5564f1
commit 3c135b2765
2 changed files with 15 additions and 10 deletions

View file

@ -3,8 +3,8 @@
#include <stdlib.h> #include <stdlib.h>
static void merge(Application* app, size_t left, size_t middle, size_t right); static int 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 mergesort_rec(Application* app, size_t left, size_t right);
static int* tmp = NULL; static int* tmp = NULL;
@ -15,19 +15,24 @@ void mergesort(Application* app)
free(tmp); free(tmp);
} }
static void mergesort_rec(Application* app, size_t left, size_t right) static int mergesort_rec(Application* app, size_t left, size_t right)
{ {
if(right > left) if(right > left)
{ {
size_t middle = (left + right) / 2; size_t middle = (left + right) / 2;
mergesort_rec(app, left, middle); if(mergesort_rec(app, left, middle))
mergesort_rec(app, middle + 1, right); return 1;
merge(app, left, middle, right); if(mergesort_rec(app, middle + 1, right))
return 1;
return merge(app, left, middle, right);
} }
return 0;
} }
static void merge(Application* app, size_t left, size_t middle, size_t right) static int merge(Application* app, size_t left, size_t middle, size_t right)
{ {
for(size_t k = left; k <= middle; k++) for(size_t k = left; k <= middle; k++)
{ {
@ -59,9 +64,9 @@ static void merge(Application* app, size_t left, size_t middle, size_t right)
k++; k++;
if(update_screen(app)) if(update_screen(app))
return; return 1;
} }
visualizer_data.array[right] = tmp[i]; visualizer_data.array[right] = tmp[i];
update_screen(app); return update_screen(app);
} }

View file

@ -46,7 +46,7 @@ int init_application(Application* app, int width, int height, const char* title)
app->active_visualizer = &histogram_visualizer; app->active_visualizer = &histogram_visualizer;
visualizer_data.array_size = 2500; visualizer_data.array_size = 1500;
visualizer_data.array = (int*)malloc(visualizer_data.array_size * sizeof(int)); visualizer_data.array = (int*)malloc(visualizer_data.array_size * sizeof(int));
// TODO: Check if NULL // TODO: Check if NULL