From 0d01b58ff148f4109b2d407f5f11152d877896c9 Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Sun, 14 Dec 2025 16:51:11 +0100 Subject: [PATCH] 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