more optimized insertion sort algorithm

This commit is contained in:
David Gailleton
2025-12-14 17:31:36 +01:00
parent 0d01b58ff1
commit e9c90e447c
3 changed files with 49 additions and 110 deletions

View File

@@ -6,87 +6,13 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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');
}
*/

4
main.c
View File

@@ -6,7 +6,7 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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