summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sorting.c27
1 files 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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 15:04:19 by dkaiser #+# #+# */
-/* Updated: 2024/04/17 12:20:26 by dkaiser ### ########.fr */
+/* Updated: 2024/04/17 14:17:55 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,10 +23,10 @@ static void presort(t_psdata *data)
max = data->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)