From ad4275c2069ae86b1bb42d226e4e4f6efef9ee87 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Tue, 9 Dec 2025 12:43:28 +0100 Subject: [PATCH 1/7] bubble algorithm WIP --- algorithms/bubble/bubble.c | 23 +++++++++++++++++++++++ main.c | 10 ++++++++-- push_swap.h | 9 ++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 algorithms/bubble/bubble.c diff --git a/algorithms/bubble/bubble.c b/algorithms/bubble/bubble.c new file mode 100644 index 0000000..b396341 --- /dev/null +++ b/algorithms/bubble/bubble.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bubble.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/12/09 11:56:23 by mteriier #+# #+# */ +/* Updated: 2025/12/09 12:43:09 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void bubble_alg(t_stacks *stacks) +{ + while (!check_order(stacks->a)) + { + if (stacks->a->value > stacks->a->next->value) + sa(stacks); + ra(stacks); + } +} diff --git a/main.c b/main.c index c5cf6e9..1327daa 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* 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/09 12:39:19 by mteriier ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,7 @@ int main(int argc, char **argv) if (argc > 1) { 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'); sa(stacks); print_all_stack(stacks->a, stacks->a, 'A'); @@ -65,6 +65,12 @@ int main(int argc, char **argv) rrr(stacks); print_all_stack(stacks->a, stacks->a, 'A'); print_all_stack(stacks->b, stacks->b, 'B'); + */ + print_all_stack(stacks->a, stacks->a, 'A'); + print_all_stack(stacks->b, stacks->b, 'B'); + bubble_alg(stacks); + print_all_stack(stacks->a, stacks->a, 'A'); + print_all_stack(stacks->b, stacks->b, 'B'); } stack_clear_all(stacks->a, stacks->a); } diff --git a/push_swap.h b/push_swap.h index 6f0b3f4..b96637a 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* 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/09 12:41:07 by mteriier ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,7 @@ void rr(t_stacks *stacks); void sa(t_stacks *stacks); void sb(t_stacks *stacks); void ss(t_stacks *stacks); + /*FUNCTION UTILS*/ t_stack *new_stack(int value); void stack_add_back(t_stack **stack, t_stack *new); @@ -47,4 +48,10 @@ t_stack *parsing(int argc, char **argv); t_stacks *init_big_stacks(int argc, char **argv); int ft_atoi(const char *nptr); +/*ALGORITHM UTILS*/ +int check_order(t_stack *stack); + +/*ALGORITHMS*/ +void bubble_alg(t_stacks *stacks); + #endif From c781d6f5cdb59bb2047606795866b0e8490584bf Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Tue, 9 Dec 2025 13:38:10 +0100 Subject: [PATCH 2/7] add print stacks function --- algorithms/bubble/bubble.c | 2 +- main.c | 11 ++-- push_swap.h | 10 ++- .../stack_len.c => stack_utils/print_stacks.c | 38 +++++++---- stack_utils/stacks_len.c | 63 +++++++++++++++++++ 5 files changed, 101 insertions(+), 23 deletions(-) rename algorithms/utils/stack_len.c => stack_utils/print_stacks.c (50%) create mode 100644 stack_utils/stacks_len.c diff --git a/algorithms/bubble/bubble.c b/algorithms/bubble/bubble.c index b396341..ac37d8d 100644 --- a/algorithms/bubble/bubble.c +++ b/algorithms/bubble/bubble.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/09 11:56:23 by mteriier #+# #+# */ -/* Updated: 2025/12/09 12:43:09 by mteriier ### ########lyon.fr */ +/* Updated: 2025/12/09 13:19:39 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ diff --git a/main.c b/main.c index 1327daa..7e77843 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */ -/* Updated: 2025/12/09 12:39:19 by mteriier ### ########lyon.fr */ +/* Updated: 2025/12/09 13:35:44 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -66,11 +66,10 @@ int main(int argc, char **argv) print_all_stack(stacks->a, stacks->a, 'A'); print_all_stack(stacks->b, stacks->b, 'B'); */ - print_all_stack(stacks->a, stacks->a, 'A'); - print_all_stack(stacks->b, stacks->b, 'B'); - bubble_alg(stacks); - print_all_stack(stacks->a, stacks->a, 'A'); - print_all_stack(stacks->b, stacks->b, 'B'); + //bubble_alg(stacks); + print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); + pb(stacks); + print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); } stack_clear_all(stacks->a, stacks->a); } diff --git a/push_swap.h b/push_swap.h index b96637a..6fc2744 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */ -/* Updated: 2025/12/09 12:41:07 by mteriier ### ########lyon.fr */ +/* Updated: 2025/12/09 13:33:04 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -47,11 +47,15 @@ void stack_clear_all(t_stack *stack, t_stack *first); t_stack *parsing(int argc, char **argv); t_stacks *init_big_stacks(int argc, char **argv); 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); +int check_order(t_stack *stack); /*ALGORITHMS*/ -void bubble_alg(t_stacks *stacks); +void bubble_alg(t_stacks *stacks); #endif diff --git a/algorithms/utils/stack_len.c b/stack_utils/print_stacks.c similarity index 50% rename from algorithms/utils/stack_len.c rename to stack_utils/print_stacks.c index c379297..261fbd4 100644 --- a/algorithms/utils/stack_len.c +++ b/stack_utils/print_stacks.c @@ -1,30 +1,42 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* stack_len.c :+: :+: :+: */ +/* print_stacks.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mteriier +#+ +:+ +#+ */ +/* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/12/09 12:05:15 by mteriier #+# #+# */ -/* Updated: 2025/12/09 12:07:29 by mteriier ### ########lyon.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 -int stack_len(t_stack *stack) +void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b) { - t_stack *first; + int a_len; + int b_len; int i; - first = stack; - if (!stack) - return (0); - i = 1; - while (stack->next != first) + i = 0; + a_len = stack_a_len(stacks); + b_len = stack_b_len(stacks); + while (i < len) { - stack = stack->next; + 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++; } - return (i); + printf("_\t_\nA\tB\n\n"); } diff --git a/stack_utils/stacks_len.c b/stack_utils/stacks_len.c new file mode 100644 index 0000000..6aa44f9 --- /dev/null +++ b/stack_utils/stacks_len.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* stacks_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} From 1463f3e14a41e27b8862f542c68a2bce69d6f712 Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Tue, 9 Dec 2025 14:23:22 +0100 Subject: [PATCH 3/7] bubble v1 --- algorithms/bubble/bubble.c | 26 +++++++++++++++++++++++--- algorithms/utils/check_order.c | 4 ++-- main.c | 9 ++++----- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/algorithms/bubble/bubble.c b/algorithms/bubble/bubble.c index ac37d8d..163f855 100644 --- a/algorithms/bubble/bubble.c +++ b/algorithms/bubble/bubble.c @@ -6,18 +6,38 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/09 11:56:23 by mteriier #+# #+# */ -/* Updated: 2025/12/09 13:19:39 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/09 14:09:07 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" +#include "unistd.h" void bubble_alg(t_stacks *stacks) { - while (!check_order(stacks->a)) + while (stack_a_len(stacks)) { if (stacks->a->value > stacks->a->next->value) + { sa(stacks); - ra(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); + } + if (check_order(stacks->a)) + return ; + bubble_alg(stacks); } diff --git a/algorithms/utils/check_order.c b/algorithms/utils/check_order.c index 07f0775..c013d48 100644 --- a/algorithms/utils/check_order.c +++ b/algorithms/utils/check_order.c @@ -6,7 +6,7 @@ /* 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/09 13:49:13 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -23,5 +23,5 @@ int check_order(t_stack *stack) return (0); stack = stack->next; } - return (0); + return (1); } diff --git a/main.c b/main.c index 7e77843..9995544 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */ -/* Updated: 2025/12/09 13:35:44 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/09 13:52:43 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -66,10 +66,9 @@ int main(int argc, char **argv) print_all_stack(stacks->a, stacks->a, 'A'); print_all_stack(stacks->b, stacks->b, 'B'); */ - //bubble_alg(stacks); - print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); - pb(stacks); - print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); + bubble_alg(stacks); + //print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); } stack_clear_all(stacks->a, stacks->a); + stack_clear_all(stacks->b, stacks->b); } From f5da9fac1f37da5a30ac05cad5f9cc6a6e333d1d Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Thu, 11 Dec 2025 18:42:33 +0100 Subject: [PATCH 4/7] New insertion algorithm WIP --- algorithms/bubble/bubble.c | 6 +- algorithms/insertion/insertion.c | 98 ++++++++++++++++++++++++++++++++ algorithms/utils/check_order.c | 2 +- algorithms/utils/compare.c | 45 +++++++++++++++ algorithms/utils/iterate.c | 40 +++++++++++++ main.c | 7 ++- push_swap.h | 7 ++- 7 files changed, 197 insertions(+), 8 deletions(-) create mode 100644 algorithms/insertion/insertion.c create mode 100644 algorithms/utils/compare.c create mode 100644 algorithms/utils/iterate.c diff --git a/algorithms/bubble/bubble.c b/algorithms/bubble/bubble.c index 163f855..cbe2bad 100644 --- a/algorithms/bubble/bubble.c +++ b/algorithms/bubble/bubble.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/09 11:56:23 by mteriier #+# #+# */ -/* Updated: 2025/12/09 14:09:07 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/09 15:56:56 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,8 @@ 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) @@ -37,7 +39,5 @@ void bubble_alg(t_stacks *stacks) write(1, "pb\n", 3); // print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); } - if (check_order(stacks->a)) - return ; bubble_alg(stacks); } diff --git a/algorithms/insertion/insertion.c b/algorithms/insertion/insertion.c new file mode 100644 index 0000000..6e4c6ec --- /dev/null +++ b/algorithms/insertion/insertion.c @@ -0,0 +1,98 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* insertion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/12/10 15:38:52 by dgaillet #+# #+# */ +/* Updated: 2025/12/12 11:30:40 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +static int nearest_below(t_stack *stack, t_stack *node, int len) +{ + t_stack *nearest; + int nearest_i; + int i; + + nearest = stack; + i = 1; + nearest_i = 0; + stack = stack->next; + while (i < len) + { + if (node->value - stack->value >= 0 + && node->value - stack->value <= node->value - nearest->value) + { + nearest_i = i; + nearest = stack; + } + stack = stack->next; + i++; + } + return (nearest_i); +} + +static int lowest_stack(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 rb_to_insert(t_stack *stack_b, t_stack *stack_a, int b_len) +{ + int i; + + i = 0; + if (is_lowest(stack_b, stack_a, b_len)) + return (lowest_stack(stack_b, b_len)); + if (is_highest(stack_b, stack_a, b_len)) + return (lowest_stack(stack_b, b_len) + 1); + return (nearest_below(stack_b, stack_a, b_len) + 1); +} + +void insertion(t_stacks *stacks, int a_len, int b_len) +{ + int temp; + + if (a_len == 0) + { + iterate_fn(stacks, b_len, &pa); + a_len = b_len; + temp = lowest_stack(stacks->a, a_len); + optimal_rotate(stacks, temp, a_len, 'a'); + return ; + } + if (b_len == 0) + pb(stacks); + else + { + temp = rb_to_insert(stacks->b, stacks->a, b_len); + optimal_rotate(stacks, temp, b_len, 'b'); + pb(stacks); + } + //write(1, "pb\n", 3); + print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); + insertion(stacks, a_len - 1, b_len + 1); +} + diff --git a/algorithms/utils/check_order.c b/algorithms/utils/check_order.c index c013d48..aae557f 100644 --- a/algorithms/utils/check_order.c +++ b/algorithms/utils/check_order.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/09 11:57:06 by mteriier #+# #+# */ -/* Updated: 2025/12/09 13:49:13 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/11 17:50:21 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ diff --git a/algorithms/utils/compare.c b/algorithms/utils/compare.c new file mode 100644 index 0000000..69b2427 --- /dev/null +++ b/algorithms/utils/compare.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* compare.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} + + diff --git a/algorithms/utils/iterate.c b/algorithms/utils/iterate.c new file mode 100644 index 0000000..9d58c89 --- /dev/null +++ b/algorithms/utils/iterate.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iterate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); + } +} diff --git a/main.c b/main.c index 9995544..b6cba03 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */ -/* Updated: 2025/12/09 13:52:43 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/11 18:32:06 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -66,8 +66,9 @@ int main(int argc, char **argv) print_all_stack(stacks->a, stacks->a, 'A'); print_all_stack(stacks->b, stacks->b, 'B'); */ - bubble_alg(stacks); - //print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); + print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); + insertion(stacks, stack_a_len(stacks), 0); + print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); } stack_clear_all(stacks->a, stacks->a); stack_clear_all(stacks->b, stacks->b); diff --git a/push_swap.h b/push_swap.h index 6fc2744..16a3f03 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */ -/* Updated: 2025/12/09 13:33:04 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/11 18:32:02 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -54,8 +54,13 @@ 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); #endif From 55363e0e6fe6a1413d4dc1ab0f8b53c0de3923eb Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Fri, 12 Dec 2025 11:40:02 +0100 Subject: [PATCH 5/7] Fix header file includes AND add write for each push swap functions --- stack_utils/push.c | 8 +++++--- stack_utils/rev_rotate.c | 14 ++++++++++---- stack_utils/rotate.c | 14 ++++++++++---- stack_utils/stack_add.c | 4 ++-- stack_utils/stack_remove.c | 4 ++-- stack_utils/swap.c | 30 ++++++++++++++++++++++++++---- 6 files changed, 55 insertions(+), 19 deletions(-) diff --git a/stack_utils/push.c b/stack_utils/push.c index 126ff89..d84ccef 100644 --- a/stack_utils/push.c +++ b/stack_utils/push.c @@ -6,12 +6,13 @@ /* 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 +#include void pa(t_stacks *stacks) { @@ -29,7 +30,7 @@ void pa(t_stacks *stacks) stacks->b = stacks->b->next; } stack_add_front(&(stacks->a), b_push); - + write(1, "pa\n", 3); } #include @@ -50,4 +51,5 @@ void pb(t_stacks *stacks) stacks->a = stacks->a->next; } stack_add_front(&(stacks->b), a_push); + write(1, "pb\n", 3); } diff --git a/stack_utils/rev_rotate.c b/stack_utils/rev_rotate.c index a8aca03..fefa099 100644 --- a/stack_utils/rev_rotate.c +++ b/stack_utils/rev_rotate.c @@ -6,26 +6,32 @@ /* 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 void rra(t_stacks *stacks) { if (stacks && stacks->a && stacks->a->previous) stacks->a = stacks->a->previous; + write(1, "rra\n", 4); } void rrb(t_stacks *stacks) { if (stacks && stacks->b && stacks->b->previous) stacks->b = stacks->b->previous; + write(1, "rrb\n", 4); } void rrr(t_stacks *stacks) { - rra(stacks); - rrb(stacks); + if (stacks && stacks->b && stacks->b->previous) + stacks->b = stacks->b->previous; + if (stacks && stacks->a && stacks->a->previous) + stacks->a = stacks->a->previous; + write(1, "rrr\n", 4); } diff --git a/stack_utils/rotate.c b/stack_utils/rotate.c index d2ce509..627240d 100644 --- a/stack_utils/rotate.c +++ b/stack_utils/rotate.c @@ -6,26 +6,32 @@ /* 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 void ra(t_stacks *stacks) { if (stacks && stacks->a && stacks->a->next) stacks->a = stacks->a->next; + write(1, "ra\n", 3); } void rb(t_stacks *stacks) { if (stacks && stacks->b && stacks->b->next) stacks->b = stacks->b->next; + write(1, "rb\n", 3); } void rr(t_stacks *stacks) { - ra(stacks); - rb(stacks); + if (stacks && stacks->a && stacks->a->next) + stacks->a = stacks->a->next; + if (stacks && stacks->b && stacks->b->next) + stacks->b = stacks->b->next; + write(1, "rr\n", 3); } diff --git a/stack_utils/stack_add.c b/stack_utils/stack_add.c index 761143a..5effe0f 100644 --- a/stack_utils/stack_add.c +++ b/stack_utils/stack_add.c @@ -6,11 +6,11 @@ /* 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 */ /* */ /* ************************************************************************** */ -#include "../push_swap.h" +#include "push_swap.h" #include t_stack *new_stack(int value) diff --git a/stack_utils/stack_remove.c b/stack_utils/stack_remove.c index 2c53bf8..cf23ef8 100644 --- a/stack_utils/stack_remove.c +++ b/stack_utils/stack_remove.c @@ -6,11 +6,11 @@ /* 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 */ /* */ /* ************************************************************************** */ -#include "../push_swap.h" +#include "push_swap.h" #include void stack_clear_all(t_stack *stack, t_stack *first) diff --git a/stack_utils/swap.c b/stack_utils/swap.c index 61fbb6b..2dad2b6 100644 --- a/stack_utils/swap.c +++ b/stack_utils/swap.c @@ -6,11 +6,12 @@ /* 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 void sa(t_stacks *stacks) { @@ -23,6 +24,7 @@ void sa(t_stacks *stacks) stock = a->value; a->value = a->next->value; a->next->value = stock; + write(1, "sa\n", 3); } void sb(t_stacks *stacks) @@ -36,10 +38,30 @@ void sb(t_stacks *stacks) stock = b->value; b->value = b->next->value; b->next->value = stock; + write(1, "sb\n", 3); } void ss(t_stacks *stacks) { - sa(stacks); - sb(stacks); + t_stack *b; + 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); } From 0d01b58ff148f4109b2d407f5f11152d877896c9 Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Sun, 14 Dec 2025 16:51:11 +0100 Subject: [PATCH 6/7] insertion sort note optimized is working --- algorithms/insertion/insertion.c | 56 ++++++++++++++++++++++++++++++-- main.c | 8 ++--- push_swap.h | 5 +-- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/algorithms/insertion/insertion.c b/algorithms/insertion/insertion.c index 6e4c6ec..54ddab8 100644 --- a/algorithms/insertion/insertion.c +++ b/algorithms/insertion/insertion.c @@ -6,12 +6,62 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/10 15:38:52 by dgaillet #+# #+# */ -/* Updated: 2025/12/12 11:30:40 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/14 16:51:05 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" +static int to_insert(t_stacks *stacks, int sorted) +{ + int i; + t_stack *a; + + i = 0; + a = stacks->a; + while (i < sorted) + { + if (stacks->b->value > a->previous->value) + return (i); + a = a->previous; + i++; + } + return (i); +} + +void insertion(t_stacks *stacks, int sorted, int len) +{ + int to_r_rotate; + + if (sorted >= len) + return ; + if (sorted == 0) + ra(stacks); + else + { + pb(stacks); + to_r_rotate = to_insert(stacks, sorted); + optimal_rotate(stacks, len - to_r_rotate - 1, len - 1, 'a'); + pa(stacks); + optimal_rotate(stacks, to_r_rotate, len, 'a'); + ra(stacks); + } + insertion(stacks, sorted + 1, len); +} +/* +void insertion(t_stacks *stacks, int len) +{ + int i; + + i = 0; + while (i < len) + { + pb(stacks); + i++; + } +} +*/ +/* static int nearest_below(t_stack *stack, t_stack *node, int len) { t_stack *nearest; @@ -92,7 +142,7 @@ void insertion(t_stacks *stacks, int a_len, int b_len) pb(stacks); } //write(1, "pb\n", 3); - print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); + //print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); insertion(stacks, a_len - 1, b_len + 1); } - +*/ diff --git a/main.c b/main.c index b6cba03..3f0ee25 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */ -/* Updated: 2025/12/11 18:32:06 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/12 14:06:41 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -66,9 +66,9 @@ int main(int argc, char **argv) print_all_stack(stacks->a, stacks->a, 'A'); 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), 0); - print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); + //print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); + insertion(stacks, 0, 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->b, stacks->b); diff --git a/push_swap.h b/push_swap.h index 16a3f03..7b7c337 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */ -/* Updated: 2025/12/11 18:32:02 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/12 14:06:25 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,7 @@ 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 a_len, int b_len); +void insertion(t_stacks *stacks, int sorted, int len); #endif From e9c90e447ce72ab1c13a688883756ab35c283c00 Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Sun, 14 Dec 2025 17:31:36 +0100 Subject: [PATCH 7/7] more optimized insertion sort algorithm --- algorithms/insertion/insertion.c | 151 +++++++++---------------------- main.c | 4 +- push_swap.h | 4 +- 3 files changed, 49 insertions(+), 110 deletions(-) diff --git a/algorithms/insertion/insertion.c b/algorithms/insertion/insertion.c index 54ddab8..f957c62 100644 --- a/algorithms/insertion/insertion.c +++ b/algorithms/insertion/insertion.c @@ -6,87 +6,13 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/10 15:38:52 by dgaillet #+# #+# */ -/* Updated: 2025/12/14 16:51:05 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/14 17:31:02 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -static int to_insert(t_stacks *stacks, int sorted) -{ - int i; - t_stack *a; - - i = 0; - a = stacks->a; - while (i < sorted) - { - if (stacks->b->value > a->previous->value) - return (i); - a = a->previous; - i++; - } - return (i); -} - -void insertion(t_stacks *stacks, int sorted, int len) -{ - int to_r_rotate; - - if (sorted >= len) - return ; - if (sorted == 0) - ra(stacks); - else - { - pb(stacks); - to_r_rotate = to_insert(stacks, sorted); - optimal_rotate(stacks, len - to_r_rotate - 1, len - 1, 'a'); - pa(stacks); - optimal_rotate(stacks, to_r_rotate, len, 'a'); - ra(stacks); - } - insertion(stacks, sorted + 1, len); -} -/* -void insertion(t_stacks *stacks, int len) -{ - int i; - - i = 0; - while (i < len) - { - pb(stacks); - i++; - } -} -*/ -/* -static int nearest_below(t_stack *stack, t_stack *node, int len) -{ - t_stack *nearest; - int nearest_i; - int i; - - nearest = stack; - i = 1; - nearest_i = 0; - stack = stack->next; - while (i < len) - { - if (node->value - stack->value >= 0 - && node->value - stack->value <= node->value - nearest->value) - { - nearest_i = i; - nearest = stack; - } - stack = stack->next; - i++; - } - return (nearest_i); -} - -static int lowest_stack(t_stack *stack, int len) +static int r_to_lowest(t_stack *stack, int len) { t_stack *lowest; int lowest_i; @@ -98,7 +24,7 @@ static int lowest_stack(t_stack *stack, int len) stack = stack->next; while (i < len) { - if (stack->value <= lowest->value) + if (stack->value < lowest->value) { lowest_i = i; lowest = stack; @@ -109,40 +35,53 @@ static int lowest_stack(t_stack *stack, int len) return (lowest_i); } -static int rb_to_insert(t_stack *stack_b, t_stack *stack_a, int b_len) +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; - if (is_lowest(stack_b, stack_a, b_len)) - return (lowest_stack(stack_b, b_len)); - if (is_highest(stack_b, stack_a, b_len)) - return (lowest_stack(stack_b, b_len) + 1); - return (nearest_below(stack_b, stack_a, b_len) + 1); -} - -void insertion(t_stacks *stacks, int a_len, int b_len) -{ - int temp; - - if (a_len == 0) + while (i < len) { - iterate_fn(stacks, b_len, &pa); - a_len = b_len; - temp = lowest_stack(stacks->a, a_len); - optimal_rotate(stacks, temp, a_len, 'a'); - return ; - } - if (b_len == 0) - pb(stacks); - else - { - temp = rb_to_insert(stacks->b, stacks->a, b_len); - optimal_rotate(stacks, temp, b_len, 'b'); pb(stacks); + i++; } - //write(1, "pb\n", 3); - //print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); - insertion(stacks, a_len - 1, b_len + 1); + ft_extra(stacks, 0, len); + optimal_rotate(stacks, r_to_lowest(stacks->a, len), len, 'a'); } -*/ diff --git a/main.c b/main.c index 3f0ee25..6ef576d 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */ -/* Updated: 2025/12/12 14:06:41 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/14 16:55:43 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -67,7 +67,7 @@ int main(int argc, char **argv) print_all_stack(stacks->b, stacks->b, 'B'); */ //print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); - insertion(stacks, 0, stack_a_len(stacks)); + insertion(stacks, stack_a_len(stacks)); //print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); } stack_clear_all(stacks->a, stacks->a); diff --git a/push_swap.h b/push_swap.h index 7b7c337..6901391 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */ -/* Updated: 2025/12/12 14:06:25 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/12/14 16:55:24 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +62,6 @@ 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 sorted, int len); +void insertion(t_stacks *stacks, int len); #endif