finish the medium need to modify and sort in a just need to do the reverse

This commit is contained in:
Maoake Teriierooiterai
2026-01-06 12:41:36 +01:00
parent ae4740c479
commit f98dce6f61
7 changed files with 143 additions and 44 deletions

View File

@@ -14,23 +14,25 @@
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range)
{
t_stack *a;
t_stack *b;
a = piles->a;
b = piles->b;
while (one_preset->nb_in > 0)
{
if (wich_path(piles, one_preset->max_range, range, 'a'))
{
while (!in_range(a->value))
while (!in_range(piles->a->value, one_preset->max_range, range))
ra(piles);
}
else
while (!in_range(a->value))
{
while (!in_range(piles->a->value, one_preset->max_range, range))
{
rra(piles);
pb(piles);
}
}
sort_little_pile(piles);
one_preset->nb_in--;
}
print_all_stack(piles->a, piles->a, 'A');
print_all_stack(piles->b, piles->b, 'B');
}
int sort_path(t_stacks *piles, int value)
@@ -39,6 +41,8 @@ int sort_path(t_stacks *piles, int value)
int end_path;
start_path = number_move_reverse(piles, value, 's');
if (start_path == 0)
return (1);
end_path = number_move_reverse(piles, value, 'e');
if (start_path < end_path)
return (1);
@@ -54,7 +58,7 @@ int number_move_reverse(t_stacks *piles, int value, char start_end)
tmp = piles->b;
if (start_end == 's')
{
while(tmp->value > value)
while(tmp->value < value)
{
tmp = tmp->next;
i++;
@@ -63,7 +67,7 @@ int number_move_reverse(t_stacks *piles, int value, char start_end)
else
{
tmp = tmp->previous;
while(tmp->value < value)
while(tmp->value > value)
{
tmp = tmp->previous;
i++;
@@ -74,9 +78,6 @@ int number_move_reverse(t_stacks *piles, int value, char start_end)
void sort_little_pile(t_stacks *piles)
{
int i;
i = 0;
if (!piles->b)
{
pb(piles);
@@ -89,5 +90,7 @@ void sort_little_pile(t_stacks *piles)
return ;
}
if (sort_path(piles, piles->a->value))
//you stop here
sort_from_left(piles);
else
sort_from_right(piles);
}

View File

@@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_utils_two.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/06 07:52:36 by mteriier #+# #+# */
/* Updated: 2026/01/06 07:52:38 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void sort_from_left(t_stacks *piles)
{
int i;
i = 0;
while (piles->a->value > piles->b->value)
{
rb(piles);
i++;
}
pb(piles);
while (i > 0)
{
rrb(piles);
i--;
}
}
void sort_from_right(t_stacks *piles)
{
int i;
i = 0;
while (piles->a->value < piles->b->previous->value)
{
rrb(piles);
i++;
}
pb(piles);
while (i >= 0)
{
rb(piles);
i--;
}
}