commit 7468848970cdd49ccab30aa8faab09b00bee0308
parent 92f5581fbd2efe376277ca010b818e33333fdef4
Author: klewer-martin <martin.cachari@gmail.com>
Date: Tue, 19 Apr 2022 01:24:55 -0300
Added sort_selector()
Added function sort_selector() which changes the selected algorithm,
it's mapped to TAB on keyboard.
After this addition a bug has been found, sometimes when you try to
restart the sorting with a new algorithm the statusbar prints "Exiting
..." (found on drw.c) and then the program hangs up.
Diffstat:
3 files changed, 25 insertions(+), 15 deletions(-)
diff --git a/drw.c b/drw.c
@@ -69,25 +69,24 @@ void drw_status_bar(Drw *drw, SAV *sav) {
}
else if(sav->status == START) {
snprintf(drw->bar_text, drw->bar_text_len - 2,
- " %-9s [%s sort] press SPACE to start sorting", sort_status_str[OK],
+ " %-8s [%s sort] press SPACE to start sorting", sort_status_str[OK],
algo_sel_str[sav->sort_algo]);
}
else if(sav->status == RUN) {
if(sav->sort_status == PAUSE)
snprintf(drw->bar_text, drw->bar_text_len - 2,
- " %-9s [%s sort] press SPACE to resume",
- sort_status_str[sav->sort_status],
- algo_sel_str[sav->sort_algo]);
+ " %-8s [%s sort] L: %ld, C: %ld, S: %ld Press SPACE to resume", sort_status_str[sav->sort_status],
+ algo_sel_str[sav->sort_algo], sav->arr->len, sav->cmps,
+ sav->swps);
else if(sav->sort_status == SORTED)
snprintf(drw->bar_text, drw->bar_text_len - 2,
- " %-9s [%s sort] done in %lds, L: %ld, C: %ld, S: %ld, extra storage used: %ld Bytes",
+ " %-8s [%s sort] L: %ld, C: %ld, S: %ld, done in %lds, extra storage used: %ld Bytes",
sort_status_str[sav->sort_status],
algo_sel_str[sav->sort_algo],
- (sav->tf - sav->ti),
- sav->arr->len, sav->cmps, sav->swps, sav->B_used);
+ sav->arr->len, sav->cmps, sav->swps, (sav->tf - sav->ti), sav->B_used);
else if(sav->sort_status == RUN)
snprintf(drw->bar_text, drw->bar_text_len - 2,
- " %-9s [%s sort] L: %ld, C: %ld, S: %ld", sort_status_str[sav->sort_status],
+ " %-8s [%s sort] L: %ld, C: %ld, S: %ld", sort_status_str[sav->sort_status],
algo_sel_str[sav->sort_algo], sav->arr->len, sav->cmps,
sav->swps);
}
diff --git a/main.c b/main.c
@@ -14,6 +14,7 @@ void check_events(Drw *, SAV *);
/* void *(*start_routine)(void *), pthread_create routine */
void *routine_wrapper(void *);
+void sort_selector(SAV *sav);
static void (*sort_handler[ALGORITHMS_COUNT])(SAV *) = {
&bubble_sort,
@@ -35,6 +36,13 @@ void *routine_wrapper(void *arg) {
return NULL;
}
+void sort_selector(SAV *sav) {
+ if(sav->sort_algo == (ALGORITHMS_COUNT - 1))
+ sav->sort_algo = BUBBLE_SORT;
+ 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 */
@@ -71,6 +79,7 @@ int main (void) {
drw_status_bar(drw, sav);
SDL_RenderPresent(drw->rend);
+ /* Print welcome message only on startup */
if(sav->status == WELCOME)
if(((toc = time(NULL)) - tic) > WELCOME_MSG_TIME)
sav->status = START;
@@ -92,11 +101,8 @@ int main (void) {
shuffle(sav->arr);
- sav->status = RUN;
+ sav->status = START;
sav->sort_status = PAUSE;
-
- /* let's start the sorting thread */
- pthread_create(&p1, NULL, &routine_wrapper, (void *)sav);
}
if(sav->sort_status == SORTED) {
@@ -107,8 +113,7 @@ int main (void) {
end:
/* check if p1 has been initialized */
- if(p1 != 0)
- pthread_join(p1, NULL);
+ if(p1 != 0) pthread_join(p1, NULL);
SAV_destroy(sav);
Drw_destroy(drw);
@@ -141,6 +146,9 @@ void check_events(Drw *drw, SAV *sav) {
case SDL_SCANCODE_MINUS:
set_sort_speed(sav, sav->sort_delay + 1);
break;
+ case SDL_SCANCODE_TAB:
+ sort_selector(sav);
+ break;
default: break;
}
break;
diff --git a/sort.c b/sort.c
@@ -246,6 +246,8 @@ void quick_sort_wrapper(SAV *sav) {
void shell_sort(SAV *sav) {
int gap, i, j, temp;
+ sav->ti = time(NULL);
+
for (gap = ((sav->arr->len) / 2); gap > 0; gap /= 2) {
sav->cmps += 1;
for (i = gap; i < sav->arr->len; i += 1) {
@@ -257,7 +259,6 @@ void shell_sort(SAV *sav) {
sav->cmps += 1;
if(sort_delay(sav) == STOP) return;
- if(sort_delay(sav) == STOP) return;
if(sort_pause(sav) == STOP) return;
}
sav->arr->v[j] = temp;
@@ -265,5 +266,7 @@ void shell_sort(SAV *sav) {
sav->cmps += 1;
}
}
+ sav->tf = time(NULL);
+
if(sav->sort_status != STOP) sav->sort_status = SORTED;
}