Merge branch 'master' into medium_algo

This commit is contained in:
Maoake Teriierooiterai
2026-01-02 13:06:01 +01:00
15 changed files with 373 additions and 29 deletions

View File

@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bubble.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 11:56:23 by mteriier #+# #+# */
/* Updated: 2025/12/09 15:56:56 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "unistd.h"
void bubble_alg(t_stacks *stacks)
{
if (check_order(stacks->a))
return ;
while (stack_a_len(stacks))
{
if (stacks->a->value > stacks->a->next->value)
{
sa(stacks);
write(1, "sa\n", 3);
}
pb(stacks);
write(1, "pb\n", 3);
// print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
}
while (stack_b_len(stacks))
{
if (stacks->b->value < stacks->b->next->value)
{
sb(stacks);
write(1, "sa\n", 3);
}
pa(stacks);
write(1, "pb\n", 3);
// print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
}
bubble_alg(stacks);
}

View File

@@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* insertion.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 15:38:52 by dgaillet #+# #+# */
/* Updated: 2025/12/14 17:31:02 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int r_to_lowest(t_stack *stack, int len)
{
t_stack *lowest;
int lowest_i;
int i;
i = 1;
lowest_i = 0;
lowest = stack;
stack = stack->next;
while (i < len)
{
if (stack->value < lowest->value)
{
lowest_i = i;
lowest = stack;
}
stack = stack->next;
i++;
}
return (lowest_i);
}
static int to_insert(t_stacks *stacks, int sorted)
{
int i;
t_stack *a;
if (is_lowest(stacks->a, stacks->b, sorted)
|| is_highest(stacks->a, stacks->b, sorted))
return (sorted - r_to_lowest(stacks->a, sorted));
i = 0;
a = stacks->a;
while (i < sorted)
{
if (stacks->b->value > a->previous->value && stacks->b->value <= a->value)
return (i);
a = a->previous;
i++;
}
return (i);
}
static void ft_extra(t_stacks *stacks, int sorted, int b_len)
{
int to_r_rotate;
if (b_len <= 0)
return ;
if (sorted <= 1)
pa(stacks);
else
{
to_r_rotate = to_insert(stacks, sorted);
optimal_rotate(stacks, sorted - to_r_rotate, sorted, 'a');
pa(stacks);
}
ft_extra(stacks, sorted + 1, b_len - 1);
}
void insertion(t_stacks *stacks, int len)
{
int i;
i = 0;
while (i < len)
{
pb(stacks);
i++;
}
ft_extra(stacks, 0, len);
optimal_rotate(stacks, r_to_lowest(stacks->a, len), len, 'a');
}

View File

@@ -6,7 +6,7 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 11:57:06 by mteriier #+# #+# */
/* Updated: 2025/12/09 12:02:28 by mteriier ### ########lyon.fr */
/* Updated: 2025/12/11 17:50:21 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
@@ -23,5 +23,5 @@ int check_order(t_stack *stack)
return (0);
stack = stack->next;
}
return (0);
return (1);
}

View File

@@ -1,30 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_len.c :+: :+: :+: */
/* compare.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 12:05:15 by mteriier #+# #+# */
/* Updated: 2025/12/09 12:07:29 by mteriier ### ########lyon.fr */
/* Created: 2025/12/11 17:46:00 by dgaillet #+# #+# */
/* Updated: 2025/12/11 17:46:22 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int stack_len(t_stack *stack)
int is_lowest(t_stack *stack, t_stack *node, int len)
{
t_stack *first;
int i;
int i;
first = stack;
if (!stack)
return (0);
i = 1;
while (stack->next != first)
i = 0;
while (i < len)
{
if (node->value > stack->value)
return (0);
stack = stack->next;
i++;
}
return (i);
return (1);
}
int is_highest(t_stack *stack, t_stack *node, int len)
{
int i;
i = 0;
while (i < len)
{
if (node->value < stack->value)
return (0);
stack = stack->next;
i++;
}
return (1);
}

View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* iterate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/11 17:49:56 by dgaillet #+# #+# */
/* Updated: 2025/12/11 18:29:05 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks))
{
while (i > 0)
{
f(stacks);
i--;
}
}
void optimal_rotate(t_stacks *stacks, int i, int len, char stack)
{
if (i && len / i >= 2)
{
if (stack == 'a')
iterate_fn(stacks, i, &ra);
else
iterate_fn(stacks, i, &rb);
}
else
{
if (stack == 'a')
iterate_fn(stacks, len - i, &rra);
else
iterate_fn(stacks, len - i, &rrb);
}
}