1 Commits

Author SHA1 Message Date
Maoake Teriierooiterai
67d7509faf all basics functions has been tested we can start the sort algorithms 2025-12-09 11:42:37 +01:00
20 changed files with 22 additions and 430 deletions

3
.gitignore vendored
View File

@@ -53,6 +53,3 @@ dkms.conf
# debug information files # debug information files
*.dwo *.dwo
# Vim swap files
*.swp

BIN
.main.c.swp Normal file

Binary file not shown.

BIN
.parsing.c.swp Normal file

Binary file not shown.

View File

@@ -1,43 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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

@@ -1,87 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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

@@ -1,27 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_order.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 11:57:06 by mteriier #+# #+# */
/* Updated: 2025/12/11 17:50:21 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int check_order(t_stack *stack)
{
t_stack *first;
first = stack;
while (stack->next != first)
{
if (stack->value > stack->next->value)
return (0);
stack = stack->next;
}
return (1);
}

View File

@@ -1,45 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* compare.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.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 is_lowest(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);
}
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

@@ -1,40 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}
}

9
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/14 16:55:43 by dgaillet ### ########lyon.fr */ /* Updated: 2025/12/09 11:39:55 by mteriier ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
if (argc > 1) if (argc > 1)
{ {
stacks = init_big_stacks(argc, argv); stacks = init_big_stacks(argc, argv);
/* print_all_stack(stacks->a, stacks->a, 'A'); print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B'); print_all_stack(stacks->b, stacks->b, 'B');
sa(stacks); sa(stacks);
print_all_stack(stacks->a, stacks->a, 'A'); print_all_stack(stacks->a, stacks->a, 'A');
@@ -65,11 +65,6 @@ int main(int argc, char **argv)
rrr(stacks); rrr(stacks);
print_all_stack(stacks->a, stacks->a, 'A'); print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B'); print_all_stack(stacks->b, stacks->b, 'B');
*/
//print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
insertion(stacks, stack_a_len(stacks));
//print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
} }
stack_clear_all(stacks->a, stacks->a); stack_clear_all(stacks->a, stacks->a);
stack_clear_all(stacks->b, stacks->b);
} }

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/14 16:55:24 by dgaillet ### ########lyon.fr */ /* Updated: 2025/12/09 10:17:27 by mteriier ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -38,7 +38,6 @@ 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);
@@ -47,21 +46,5 @@ void stack_clear_all(t_stack *stack, t_stack *first);
t_stack *parsing(int argc, char **argv); t_stack *parsing(int argc, char **argv);
t_stacks *init_big_stacks(int argc, char **argv); t_stacks *init_big_stacks(int argc, char **argv);
int ft_atoi(const char *nptr); int ft_atoi(const char *nptr);
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

BIN
stack_utils/.push.c.swp Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -1,42 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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,13 +6,12 @@
/* 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/12 11:39:33 by dgaillet ### ########lyon.fr */ /* Updated: 2025/12/09 11:36:36 by mteriier ### ########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)
{ {
@@ -30,7 +29,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);
} }
#include <stdio.h> #include <stdio.h>
@@ -51,5 +50,4 @@ 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,32 +6,26 @@
/* 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/12 11:39:25 by dgaillet ### ########lyon.fr */ /* Updated: 2025/12/08 19:37:26 by mteriier ### ########.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)
{ {
if (stacks && stacks->b && stacks->b->previous) rra(stacks);
stacks->b = stacks->b->previous; rrb(stacks);
if (stacks && stacks->a && stacks->a->previous)
stacks->a = stacks->a->previous;
write(1, "rrr\n", 4);
} }

View File

@@ -6,32 +6,26 @@
/* 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/12 11:39:17 by dgaillet ### ########lyon.fr */ /* Updated: 2025/12/08 19:37:44 by mteriier ### ########.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)
{ {
if (stacks && stacks->a && stacks->a->next) ra(stacks);
stacks->a = stacks->a->next; rb(stacks);
if (stacks && stacks->b && stacks->b->next)
stacks->b = stacks->b->next;
write(1, "rr\n", 3);
} }

View File

@@ -6,11 +6,11 @@
/* 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/12 11:39:09 by dgaillet ### ########lyon.fr */ /* Updated: 2025/12/09 08:51:33 by mteriier ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "../push_swap.h"
#include <stdlib.h> #include <stdlib.h>
t_stack *new_stack(int value) t_stack *new_stack(int value)

View File

@@ -6,11 +6,11 @@
/* 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/12 11:39:02 by dgaillet ### ########lyon.fr */ /* Updated: 2025/12/08 19:40:01 by mteriier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "../push_swap.h"
#include <stdlib.h> #include <stdlib.h>
void stack_clear_all(t_stack *stack, t_stack *first) void stack_clear_all(t_stack *stack, t_stack *first)

View File

@@ -1,63 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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,12 +6,11 @@
/* 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/12 11:38:52 by dgaillet ### ########lyon.fr */ /* Updated: 2025/12/08 19:40:49 by mteriier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "../push_swap.h"
#include <unistd.h>
void sa(t_stacks *stacks) void sa(t_stacks *stacks)
{ {
@@ -24,7 +23,6 @@ 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)
@@ -38,30 +36,10 @@ 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)
{ {
t_stack *b; sa(stacks);
t_stack *a; sb(stacks);
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);
} }