From f5da9fac1f37da5a30ac05cad5f9cc6a6e333d1d Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Thu, 11 Dec 2025 18:42:33 +0100 Subject: [PATCH] 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