From: Dominik Kaiser Date: Wed, 17 Apr 2024 09:51:17 +0000 (+0200) Subject: Add score calculation X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=26e76f9d7ad46948b0548c41375f81cacf20b47c;p=42%2Fpush_swap.git Add score calculation --- diff --git a/sorting.c b/sorting.c index ca21757..a682280 100644 --- a/sorting.c +++ b/sorting.c @@ -6,7 +6,7 @@ /* By: dkaiser b->size + 1) / 2) + moves_to_top = pos; + else + moves_to_top = data->b->size - pos; + i = 0; + while (i < data->a->size && data->a->stack[i] < data->b->stack[pos]) + i++; + if (i < (data->a->size + 1) / 2) + moves_to_spot = 2 * i + 1; + else + moves_to_spot = 2 * ((data->a->size - i) + 1); + return moves_to_top + moves_to_spot; +} + +static void move_to_right_spot(t_psdata *data, int idx) +{ + +} + +static void scoresort(t_psdata *data) +{ + int *scores; + int i; + int min_score; + + scores = malloc(sizeof(int) * data->b->size); + // Error if allocation fails + i = 0; + while (i < data->b->size) + { + scores[i] = calculate_score(data, i); + i++; + } + i = 0; + min_score = 0; + while (i < data->b->size) + { + if (scores[i] < scores[min_score]) + min_score = i; + i++; + } + move_to_right_spot(data, min_score); + + free(scores); + if (data->b->size > 0) + scoresort(data); +} + void stack_sort(t_psdata *data) { presort(data); + scoresort(data); }