Push_swap/srcs/sort.c

88 lines
2.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:00:34 by lfirmin #+# #+# */
/* Updated: 2024/11/03 18:13:21 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/pushswap.h"
void sort(t_stack *stack_a, t_stack *stack_b)
{
int median;
t_node *current;
current = stack_a->top;
median = get_median(stack_a, stack_a->size);
if (stack_a->size == 2)
sort_2(stack_a);
else
{
while (current->next->next->next)
{
push(stack_a, stack_b);
ft_printf_fd(1, "pb\n");
if (current->value > median)
{
rotate(stack_b);
ft_printf_fd(1, "rb\n");
}
current = stack_a->top;
}
sort_3(stack_a);
while (stack_b->size != 0)
make_move(stack_a, stack_b, calc_cheaper(stack_a, stack_b));
final_sort(stack_a);
}
}
void final_sort(t_stack *stack_a)
{
int index_min;
index_min = return_index(stack_a, calc_index_min(stack_a));
make_move_a(stack_a, calc_move_stack_2(stack_a, index_min));
}
void opti(int *move)
{
if (move[0] > 0 && move[1] > 0)
{
if (move[0] > move[1])
{
move[2] = move[1];
move[0] = move[0] - move[1];
move[1] = 0;
}
else
{
move[2] = move[0];
move[1] = move[1] - move[0];
move[0] = 0;
}
}
else if (move[0] < 0 && move[1] < 0)
opti_2(move);
}
void opti_2(int *move)
{
if (move[0] < move[1])
{
move[2] = move[0];
move[1] = move[1] - move[0];
move[0] = 0;
}
else
{
move[2] = move[1];
move[0] = move[0] - move[1];
move[1] = 0;
}
}