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> +#+ +:+ +#+ */ /* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 11:57:06 by mteriier #+# #+# */ /* 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); return (0);
stack = stack->next; 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 #+# #+# */ /* Created: 2025/12/11 17:46:00 by dgaillet #+# #+# */
/* Updated: 2025/12/09 12:07:29 by mteriier ### ########lyon.fr */ /* Updated: 2025/12/11 17:46:22 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #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; i = 0;
if (!stack) while (i < len)
return (0);
i = 1;
while (stack->next != first)
{ {
if (node->value > stack->value)
return (0);
stack = stack->next; stack = stack->next;
i++; 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);
}
}

2
main.c
View File

@@ -6,7 +6,7 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */ /* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */ /* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */
/* Updated: 2025/12/09 11:39:55 by mteriier ### ########lyon.fr */ /* Updated: 2025/12/14 16:55:43 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */ /* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */
/* Updated: 2025/12/09 10:17:27 by mteriier ### ########lyon.fr */ /* Updated: 2025/12/14 16:55:24 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -45,6 +45,7 @@ void rr(t_stacks *stacks);
void sa(t_stacks *stacks); void sa(t_stacks *stacks);
void sb(t_stacks *stacks); void sb(t_stacks *stacks);
void ss(t_stacks *stacks); void ss(t_stacks *stacks);
/*FUNCTION UTILS*/ /*FUNCTION UTILS*/
t_stack *new_stack(int value); t_stack *new_stack(int value);
void stack_add_back(t_stack **stack, t_stack *new); void stack_add_back(t_stack **stack, t_stack *new);
@@ -87,5 +88,21 @@ int test2(char **argv);
t_stack *parsing2(int *tab, int len); t_stack *parsing2(int *tab, int len);
t_stacks *init_big_stacks2(int *tab, int len); t_stacks *init_big_stacks2(int *tab, int len);
void print_tabs(t_tab *preset); void print_tabs(t_tab *preset);
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b);
int stack_a_len(t_stacks *stacks);
int stack_b_len(t_stacks *stacks);
int highest_stack_len(t_stacks *stacks);
/*ALGORITHM UTILS*/
int check_order(t_stack *stack);
void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks));
int is_lowest(t_stack *stack, t_stack *node, int len);
int is_highest(t_stack *stack, t_stack *node, int len);
void optimal_rotate(t_stacks *stacks, int i, int len, char stack);
/*ALGORITHMS*/
void bubble_alg(t_stacks *stacks);
//void insertion(t_stacks *stacks, int a_len, int b_len);
void insertion(t_stacks *stacks, int len);
#endif #endif

View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_stacks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 13:10:00 by dgaillet #+# #+# */
/* Updated: 2025/12/09 13:36:03 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdio.h>
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b)
{
int a_len;
int b_len;
int i;
i = 0;
a_len = stack_a_len(stacks);
b_len = stack_b_len(stacks);
while (i < len)
{
if (a_len >= len - i)
{
printf("%d", a->value);
a = a->next;
}
printf("\t");
if (b_len >= len - i)
{
printf("%d", b->value);
b = b->next;
}
printf("\n");
i++;
}
printf("_\t_\nA\tB\n\n");
}

View File

@@ -6,12 +6,13 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */ /* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 15:09:40 by mteriier #+# #+# */ /* Created: 2025/12/08 15:09:40 by mteriier #+# #+# */
/* Updated: 2025/12/09 11:36:36 by mteriier ### ########lyon.fr */ /* Updated: 2025/12/12 11:39:33 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
void pa(t_stacks *stacks) void pa(t_stacks *stacks)
{ {
@@ -29,6 +30,7 @@ void pa(t_stacks *stacks)
stacks->b = stacks->b->next; stacks->b = stacks->b->next;
} }
stack_add_front(&(stacks->a), b_push); stack_add_front(&(stacks->a), b_push);
write(1, "pa\n", 3);
} }
void pb(t_stacks *stacks) void pb(t_stacks *stacks)
@@ -47,4 +49,5 @@ void pb(t_stacks *stacks)
stacks->a = stacks->a->next; stacks->a = stacks->a->next;
} }
stack_add_front(&(stacks->b), a_push); stack_add_front(&(stacks->b), a_push);
write(1, "pb\n", 3);
} }

View File

@@ -6,26 +6,32 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 14:43:45 by dgaillet #+# #+# */ /* Created: 2025/12/08 14:43:45 by dgaillet #+# #+# */
/* Updated: 2025/12/08 19:37:26 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:39:25 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
#include <unistd.h>
void rra(t_stacks *stacks) void rra(t_stacks *stacks)
{ {
if (stacks && stacks->a && stacks->a->previous) if (stacks && stacks->a && stacks->a->previous)
stacks->a = stacks->a->previous; stacks->a = stacks->a->previous;
write(1, "rra\n", 4);
} }
void rrb(t_stacks *stacks) void rrb(t_stacks *stacks)
{ {
if (stacks && stacks->b && stacks->b->previous) if (stacks && stacks->b && stacks->b->previous)
stacks->b = stacks->b->previous; stacks->b = stacks->b->previous;
write(1, "rrb\n", 4);
} }
void rrr(t_stacks *stacks) void rrr(t_stacks *stacks)
{ {
rra(stacks); if (stacks && stacks->b && stacks->b->previous)
rrb(stacks); stacks->b = stacks->b->previous;
if (stacks && stacks->a && stacks->a->previous)
stacks->a = stacks->a->previous;
write(1, "rrr\n", 4);
} }

View File

@@ -6,26 +6,32 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 14:32:10 by dgaillet #+# #+# */ /* Created: 2025/12/08 14:32:10 by dgaillet #+# #+# */
/* Updated: 2025/12/08 19:37:44 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:39:17 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
#include <unistd.h>
void ra(t_stacks *stacks) void ra(t_stacks *stacks)
{ {
if (stacks && stacks->a && stacks->a->next) if (stacks && stacks->a && stacks->a->next)
stacks->a = stacks->a->next; stacks->a = stacks->a->next;
write(1, "ra\n", 3);
} }
void rb(t_stacks *stacks) void rb(t_stacks *stacks)
{ {
if (stacks && stacks->b && stacks->b->next) if (stacks && stacks->b && stacks->b->next)
stacks->b = stacks->b->next; stacks->b = stacks->b->next;
write(1, "rb\n", 3);
} }
void rr(t_stacks *stacks) void rr(t_stacks *stacks)
{ {
ra(stacks); if (stacks && stacks->a && stacks->a->next)
rb(stacks); stacks->a = stacks->a->next;
if (stacks && stacks->b && stacks->b->next)
stacks->b = stacks->b->next;
write(1, "rr\n", 3);
} }

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 15:52:40 by dgaillet #+# #+# */ /* Created: 2025/12/08 15:52:40 by dgaillet #+# #+# */
/* Updated: 2025/12/09 08:51:33 by mteriier ### ########lyon.fr */ /* Updated: 2025/12/12 11:39:09 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 16:05:27 by dgaillet #+# #+# */ /* Created: 2025/12/08 16:05:27 by dgaillet #+# #+# */
/* Updated: 2025/12/08 19:40:01 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:39:02 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

63
stack_utils/stacks_len.c Normal file
View File

@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stacks_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 13:11:50 by dgaillet #+# #+# */
/* Updated: 2025/12/09 13:19:21 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int stack_a_len(t_stacks *stacks)
{
t_stack *first;
t_stack *a_stack;
int len;
a_stack = stacks->a;
first = a_stack;
if (!a_stack)
return (0);
len = 1;
while (a_stack->next != first)
{
a_stack = a_stack->next;
len++;
}
return (len);
}
int stack_b_len(t_stacks *stacks)
{
t_stack *first;
t_stack *b_stack;
int len;
b_stack = stacks->b;
first = b_stack;
if (!b_stack)
return (0);
len = 1;
while (b_stack->next != first)
{
b_stack = b_stack->next;
len++;
}
return (len);
}
int highest_stack_len(t_stacks *stacks)
{
int a_len;
int b_len;
a_len = stack_a_len(stacks);
b_len = stack_b_len(stacks);
if (a_len > b_len)
return (a_len);
return (b_len);
}

View File

@@ -6,11 +6,12 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */ /* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 14:48:44 by mteriier #+# #+# */ /* Created: 2025/12/08 14:48:44 by mteriier #+# #+# */
/* Updated: 2025/12/08 19:40:49 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:38:52 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
#include <unistd.h>
void sa(t_stacks *stacks) void sa(t_stacks *stacks)
{ {
@@ -23,6 +24,7 @@ void sa(t_stacks *stacks)
stock = a->value; stock = a->value;
a->value = a->next->value; a->value = a->next->value;
a->next->value = stock; a->next->value = stock;
write(1, "sa\n", 3);
} }
void sb(t_stacks *stacks) void sb(t_stacks *stacks)
@@ -36,10 +38,30 @@ void sb(t_stacks *stacks)
stock = b->value; stock = b->value;
b->value = b->next->value; b->value = b->next->value;
b->next->value = stock; b->next->value = stock;
write(1, "sb\n", 3);
} }
void ss(t_stacks *stacks) void ss(t_stacks *stacks)
{ {
sa(stacks); t_stack *b;
sb(stacks); t_stack *a;
int stock;
if (!stacks)
return ;
if (stacks->b && stacks->b->next)
{
b = stacks->b;
stock = b->value;
b->value = b->next->value;
b->next->value = stock;
}
if (stacks->a && stacks->a->next)
{
a = stacks->a;
stock = a->value;
a->value = a->next->value;
a->next->value = stock;
}
write(1, "ss\n", 3);
} }