From cd2231f596dd945dbf37e7d7b260d6d8950ffce7 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 17 Apr 2024 14:19:37 +0200 Subject: [PATCH] Change sorting direction (still not fully working) Somehow the 0 is still not sorted and in some cases 1 as well. TODO: Fix this --- sorting.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/sorting.c b/sorting.c index c4d3644..dfaafe2 100644 --- a/sorting.c +++ b/sorting.c @@ -6,7 +6,7 @@ /* By: dkaiser a->stack[0]; while (size--) { - if (data->a->stack[0] < max) + if (data->a->stack[0] > max) { run_command(data, PB); - if (data->b->stack[0] > pivot) + if (data->b->stack[0] < pivot) run_command(data, RB); } else @@ -48,7 +48,7 @@ static int calculate_score(t_psdata *data, int pos) else moves_to_top = data->b->size - pos; i = 0; - while (i < data->a->size && data->a->stack[i] < data->b->stack[pos]) + 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; @@ -63,7 +63,7 @@ static void move_to_top(t_psdata *data, int idx) { if (idx < (data->b->size + 1) / 2) { - while (idx--) + while (--idx > 0) run_command(data, RB); } else @@ -74,19 +74,26 @@ static void move_to_top(t_psdata *data, int idx) } } -static void move_to_spot(t_psdata *data) +static void move_to_spot(t_psdata *data, int idx) { int pos; int i; pos = 0; - while (pos < data->a->size && data->b->stack[0] < data->a->stack[pos]) + while (pos < data->a->size && data->b->stack[0] > data->a->stack[pos]) pos++; - if (pos < (data->a->size + 1) / 2) + if (pos >= data->a->size) + { + move_to_top(data, idx); + run_command(data, PA); + run_command(data, RA); + } + else if (pos < (data->a->size + 1) / 2) { i = pos; while (i--) run_command(data, RA); + move_to_top(data, idx); run_command(data, PA); i = pos; while (i--) @@ -97,6 +104,7 @@ static void move_to_spot(t_psdata *data) i = data->a->size - pos; while (i--) run_command(data, RRA); + move_to_top(data, idx); run_command(data, PA); i = data->a->size - pos; while (i--) @@ -126,8 +134,7 @@ static void scoresort(t_psdata *data) min_score = i; i++; } - move_to_top(data, min_score); - move_to_spot(data); + move_to_spot(data, min_score); free(scores); if (data->b->size > 0) -- 2.47.2