mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
New insertion algorithm WIP
This commit is contained in:
committed by
David Gailleton
parent
1463f3e14a
commit
f5da9fac1f
@@ -6,7 +6,7 @@
|
|||||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/09 11:56:23 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)
|
void bubble_alg(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
|
if (check_order(stacks->a))
|
||||||
|
return ;
|
||||||
while (stack_a_len(stacks))
|
while (stack_a_len(stacks))
|
||||||
{
|
{
|
||||||
if (stacks->a->value > stacks->a->next->value)
|
if (stacks->a->value > stacks->a->next->value)
|
||||||
@@ -37,7 +39,5 @@ void bubble_alg(t_stacks *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);
|
||||||
}
|
}
|
||||||
if (check_order(stacks->a))
|
|
||||||
return ;
|
|
||||||
bubble_alg(stacks);
|
bubble_alg(stacks);
|
||||||
}
|
}
|
||||||
|
|||||||
98
algorithms/insertion/insertion.c
Normal file
98
algorithms/insertion/insertion.c
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* insertion.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/09 11:57:06 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
45
algorithms/utils/compare.c
Normal file
45
algorithms/utils/compare.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* compare.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
40
algorithms/utils/iterate.c
Normal file
40
algorithms/utils/iterate.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* iterate.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
7
main.c
7
main.c
@@ -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/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->a, stacks->a, 'A');
|
||||||
print_all_stack(stacks->b, stacks->b, 'B');
|
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->a, stacks->a);
|
||||||
stack_clear_all(stacks->b, stacks->b);
|
stack_clear_all(stacks->b, stacks->b);
|
||||||
|
|||||||
@@ -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/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*/
|
/*ALGORITHM UTILS*/
|
||||||
int check_order(t_stack *stack);
|
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*/
|
/*ALGORITHMS*/
|
||||||
void bubble_alg(t_stacks *stacks);
|
void bubble_alg(t_stacks *stacks);
|
||||||
|
void insertion(t_stacks *stacks, int a_len, int b_len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user