commit fca225c2996cfd809ee096bbe8a8cf38f4f9effb
parent 7468848970cdd49ccab30aa8faab09b00bee0308
Author: Martin J. Klöckner <64109770+klewer-martin@users.noreply.github.com>
Date: Wed, 20 Apr 2022 20:54:24 -0300
Merge pull request #10 from klewer-martin/quick_sort_fix
Fixed QUICK_SORT bug
Diffstat:
8 files changed, 49 insertions(+), 36 deletions(-)
diff --git a/drw.c b/drw.c
@@ -3,22 +3,6 @@
#include <assert.h>
static SDL_Rect rect;
-static const char *algo_sel_str[ALGORITHMS_COUNT] = {
- "bubble",
- "improved bubble",
- "insertion",
- "merge",
- "quick",
- "shell"
-};
-
-static const char *sort_status_str[STATUS_MAX] = {
- "READY",
- "SORTING",
- "PAUSED",
- "SORTED",
- "STOPPED"
-};
void drw_element_color(Drw *drw, int x, int y, int h, unsigned int col) {
rect.x = x + drw->x_border; /* bottom left + x */
@@ -28,7 +12,6 @@ void drw_element_color(Drw *drw, int x, int y, int h, unsigned int col) {
SDL_SetRenderDrawColor(drw->rend, UNHEX(col));
SDL_RenderFillRect(drw->rend, &rect);
-
/* printf("INFO: color: #%02X%02X%02X%02X\n", UNHEX(col)); */
/* Simulate shadows around rectangles */
diff --git a/drw.h b/drw.h
@@ -44,6 +44,23 @@ typedef struct {
char *bar_text;
} Drw;
+static char * const algo_sel_str[ALGORITHMS_COUNT] = {
+ "bubble",
+ "improved bubble",
+ "insertion",
+ "merge",
+ "quick",
+ "shell"
+};
+
+static char * const sort_status_str[STATUS_MAX] = {
+ "READY",
+ "SORTING",
+ "PAUSED",
+ "SORTED",
+ "STOPPED"
+};
+
status_t Drw_new(Drw **drw);
void Drw_destroy(Drw *drw);
diff --git a/main.c b/main.c
@@ -38,11 +38,10 @@ void *routine_wrapper(void *arg) {
void sort_selector(SAV *sav) {
if(sav->sort_algo == (ALGORITHMS_COUNT - 1))
- sav->sort_algo = BUBBLE_SORT;
+ sav->sort_algo = 0;
else sav->sort_algo++;
}
-/* TODO: Find bug when restarting the sort; statusbar prints "Exiting ..." */
/* TODO: Support random, reversed, in_order arrays */
/* TODO: Support command line arguments */
/* TODO: Support sound */
@@ -62,7 +61,7 @@ int main (void) {
shuffle(sav->arr);
/* selecting the sorting algorithm */
- sav->sort_algo = SHELL_SORT;
+ sav->sort_algo = QUICK_SORT;
sav->status = WELCOME;
sav->sort_status = PAUSE;
@@ -87,6 +86,7 @@ int main (void) {
if((sav->status == START) || (sav->status == WELCOME)) {
if(sav->sort_status == RUN) {
sav->status = RUN;
+
/* start sorting thread */
pthread_create(&p1, NULL, &routine_wrapper, (void *)sav);
}
@@ -98,16 +98,14 @@ int main (void) {
pthread_join(p1, NULL);
reset_sort_stats(sav);
-
shuffle(sav->arr);
sav->status = START;
sav->sort_status = PAUSE;
}
- if(sav->sort_status == SORTED) {
+ if(sav->sort_status == SORTED)
sav->sel = sav->cmp = ARR_LEN + 1;
- }
}
end:
diff --git a/sav.c b/sav.c
@@ -31,7 +31,8 @@ status_t SAV_new(SAV **sav) {
if((*sav)->arr == NULL) {
return ERROR_MEMORY_ALLOC;
}
- return 0;
+
+ return OK;
}
void SAV_destroy(SAV *sav) {
diff --git a/sav.h b/sav.h
@@ -23,7 +23,6 @@ typedef enum {
typedef struct {
Arr *arr;
size_t sel, cmp, cmps, swps, its, B_used;
- /* clock_t ti, tf; */
time_t ti, tf;
status_t status, prev_status, sort_status;
sort_t sort_algo;
diff --git a/sdl_extra.c b/sdl_extra.c
@@ -7,11 +7,11 @@ status_t SDL_setup(SDL_Window **win, SDL_Renderer **rend) {
*win = SDL_CreateWindow(
WIN_TITLE,
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- 0,
- 0,
- SDL_WINDOW_RESIZABLE | SDL_WINDOW_UTILITY
+ SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED,
+ 800,
+ 600,
+ SDL_WINDOW_RESIZABLE
);
*rend = SDL_CreateRenderer(*win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
diff --git a/sort.c b/sort.c
@@ -16,13 +16,10 @@ void set_sort_speed(SAV *sav, size_t new_value) {
status_t sort_delay(const SAV *sav) {
size_t i;
- for(i = 0; i < sav->sort_delay; i++) {
+ for(i = 0; i < sav->sort_delay; i++, SDL_Delay(1))
if((sav->sort_status == STOP) || (sav->status == STOP))
return STOP;
- SDL_Delay(1);
- }
-
return OK;
}
@@ -30,7 +27,7 @@ status_t sort_pause(const SAV *sav) {
while(sav->sort_status == PAUSE)
if(sort_delay(sav) == STOP) return STOP;
- return OK;
+ return sav->sort_status == STOP ? STOP : OK;
}
void insertion_sort(SAV *sav) {
@@ -195,6 +192,7 @@ void merge_sort_wrapper(SAV *sav) {
sav->ti = time(NULL);
merge_sort(sav, 0, sav->arr->len);
sav->tf = time(NULL);
+
sav->sel = sav->arr->len + 1;
if(sav->status != STOP) sav->sort_status = SORTED;
sav->sel = sav->cmp = ARR_MAX + 1;
@@ -211,6 +209,7 @@ void quick_sort_partition(SAV *sav, int low, int *middle, int high) {
swap(&(sav->arr->v[i++]), &(sav->arr->v[j]));
sav->swps += 1;
}
+ sav->cmp = j;
if(sort_delay(sav) == STOP) return;
if(sort_pause(sav) == STOP) return;
@@ -238,7 +237,6 @@ void quick_sort_wrapper(SAV *sav) {
sav->ti = time(NULL);
quick_sort(sav, 0, sav->arr->len - 1);
- printf("SORT: sorting array done\n");
if(sav->sort_status != STOP) sav->sort_status = SORTED;
sav->sel = sav->cmp = ARR_MAX + 1;
sav->tf = time(NULL);
diff --git a/status.h b/status.h
@@ -19,4 +19,21 @@ typedef enum {
STATUS_MAX
} status_t;
+static char * const status_string[STATUS_MAX] = {
+ "OK",
+ "RUN",
+ "PAUSE",
+ "SORTED",
+ "STOP",
+ "UPDATE",
+ "ERROR_MEMORY_ALLOC",
+ "ERROR_OPENING_FONT",
+ "ERROR_SDL_FONT_INIT",
+ "ERROR_NULL_POINTER",
+ "ERROR_DRW",
+ "START",
+ "RESTART",
+ "WELCOME"
+};
+
#endif