insertion sort note optimized is working

This commit is contained in:
David Gailleton
2025-12-14 16:51:11 +01:00
parent a68bcd2503
commit 0d01b58ff1
3 changed files with 60 additions and 9 deletions

View File

@@ -6,12 +6,62 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 15:38:52 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" #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) static int nearest_below(t_stack *stack, t_stack *node, int len)
{ {
t_stack *nearest; t_stack *nearest;
@@ -92,7 +142,7 @@ void insertion(t_stacks *stacks, int a_len, int b_len)
pb(stacks); pb(stacks);
} }
//write(1, "pb\n", 3); //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); insertion(stacks, a_len - 1, b_len + 1);
} }
*/

8
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/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->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); //print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
insertion(stacks, stack_a_len(stacks), 0); insertion(stacks, 0, stack_a_len(stacks));
print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); //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); 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/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*/ /*ALGORITHMS*/
void bubble_alg(t_stacks *stacks); 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 #endif