commit 580a98a42c5d1edeb4cd325b053fdaa217652bf4
parent 7bd2407f7d44290ca008ff4d0680db7ab14245fe
Author: klewer-martin <martin.cachari@gmail.com>
Date: Tue, 12 Apr 2022 00:41:13 -0300
Added extra storage counter in bytes
Added an extra variable to store size in bytes of extra storage used by
algorithms (i.e. mergesort)
Diffstat:
6 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/drw.c b/drw.c
@@ -67,10 +67,10 @@ drw_status_bar(Drw *drw, SAV *sav) {
drw_text(drw, drw->bar_text, 0, drw->h - drw->font_size - 5);
} else if(sav->status == SORTED) {
snprintf(drw->bar_text, drw->bar_text_len - 2,
- "SORTED (%s sort) done in %.2fs, L: %ld, C: %ld, S: %ld, I: %ld",
+ "SORTED (%s sort) done in %.2fs, L: %ld, C: %ld, S: %ld, I: %ld, extra storage used: %ld Bytes",
algo_strings[sav->sel_algo],
(double)(sav->tf - sav->ti) / CLOCKS_PER_SEC,
- sav->arr->len, sav->cmps, sav->swps, sav->its);
+ sav->arr->len, sav->cmps, sav->swps, sav->its, sav->B_used);
drw_text(drw, drw->bar_text, 0, drw->h - drw->font_size - 5);
}
@@ -125,7 +125,17 @@ status_t DRW_New(SDL_Renderer *rend, SDL_Window *win, Drw **drw) {
else if((*drw)->h < WIN_MIN_H)
(*drw)->h = WIN_MIN_H;
- (*drw)->bar_text_len = (*drw)->w / (*drw)->font_size;
+ {
+ int w_text, h_text;
+ TTF_SizeText(font,
+ "SORTED (XXXXXXXXXXs sort) done in XXX.Xs, L: XXXXX,\
+ C: XXXXXX, S: XXXXXX, I: XXXXXX, storage used: XXXXXX Bytes",
+ &w_text, &h_text);
+
+ (*drw)->bar_text_len = w_text;
+ }
+
+
(*drw)->bar_text = (char *)malloc(sizeof(char) * (*drw)->bar_text_len);
if((*drw)->bar_text == NULL) return ERROR_MEMORY_ALLOC;
diff --git a/main.c b/main.c
@@ -58,7 +58,7 @@ main (void) {
pthread_create(&p1, NULL, &routine_wrapper, (void *)sav);
/* selecting the sorting algorithms */
- sav->sel_algo = MERGE_SORT;
+ sav->sel_algo = INSERTION_SORT;
/* main loop */
while(sav->status != STOP) {
diff --git a/sav.c b/sav.c
@@ -9,7 +9,7 @@ status_t SAV_New(SAV **sav) {
if((*sav = (SAV *)malloc(sizeof(SAV))) == NULL)
return ERROR_MEMORY_ALLOC;
- (*sav)->sel = (*sav)->cmps = (*sav)->swps = (*sav)->its = 0;
+ (*sav)->sel = (*sav)->cmps = (*sav)->swps = (*sav)->its = (*sav)->B_used = 0;
(*sav)->cmp = ARR_MAX + 1;
(*sav)->status = RUN;
(*sav)->sel_algo = SORT_MAX_ALGORITHMS;
diff --git a/sav.h b/sav.h
@@ -15,7 +15,7 @@ typedef enum {
typedef struct {
Arr *arr;
- size_t sel, cmp, cmps, swps, its;
+ size_t sel, cmp, cmps, swps, its, B_used;
clock_t ti, tf;
status_t status;
sort_t sel_algo;
diff --git a/sort.c b/sort.c
@@ -75,6 +75,9 @@ void merge(SAV *sav, int low, int middle, int high) {
int B[n1], C[n2];
+ sav->B_used += n1;
+ sav->B_used += n2;
+
/* B holds middle low array */
for(i = low, j = 0; i < middle; i++, j++)
B[j] = sav->arr->v[i];
diff --git a/util.h b/util.h
@@ -4,14 +4,14 @@
#include <stdio.h>
#include <stdlib.h>
-#define ARR_LEN 120
+#define ARR_LEN 128
#define ARR_MAX 500
#define X_BORDER 40
#define Y_BORDER 40
#define TOP_BORDER 50
-#define RECT_WIDTH 6
+#define RECT_WIDTH 5
typedef enum {
OK = 0,