mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
Compare commits
2 Commits
simple_alg
...
pre_sortin
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
805d592160 | ||
|
|
e2495fa62f |
@@ -6,35 +6,12 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/10 15:38:52 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/14 17:31:02 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/12/15 14:45:54 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
static int r_to_lowest(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 to_insert(t_stacks *stacks, int sorted)
|
||||
{
|
||||
int i;
|
||||
@@ -76,6 +53,13 @@ void insertion(t_stacks *stacks, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (len == 3)
|
||||
{
|
||||
sort_three_a(stacks);
|
||||
return ;
|
||||
}
|
||||
if (check_order(stacks->a))
|
||||
return ;
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* 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 */
|
||||
/* Updated: 2025/12/15 14:44:56 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -23,6 +23,8 @@ void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks))
|
||||
|
||||
void optimal_rotate(t_stacks *stacks, int i, int len, char stack)
|
||||
{
|
||||
if (i == len)
|
||||
return ;
|
||||
if (i && len / i >= 2)
|
||||
{
|
||||
if (stack == 'a')
|
||||
@@ -30,7 +32,7 @@ void optimal_rotate(t_stacks *stacks, int i, int len, char stack)
|
||||
else
|
||||
iterate_fn(stacks, i, &rb);
|
||||
}
|
||||
else
|
||||
else if (i)
|
||||
{
|
||||
if (stack == 'a')
|
||||
iterate_fn(stacks, len - i, &rra);
|
||||
|
||||
51
algorithms/utils/pre_sort.c
Normal file
51
algorithms/utils/pre_sort.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pre_sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/15 14:21:20 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/15 14:59:52 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int r_to_lowest(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);
|
||||
}
|
||||
|
||||
void sort_three_a(t_stacks *stacks)
|
||||
{
|
||||
if (check_order(stacks->a))
|
||||
return ;
|
||||
if (stacks->a->value > stacks->a->next->value)
|
||||
sa(stacks);
|
||||
optimal_rotate(stacks, r_to_lowest(stacks->a, 3), 3, 'a');
|
||||
if (stacks->a->next->value > stacks->a->previous->value)
|
||||
{
|
||||
ra(stacks);
|
||||
sa(stacks);
|
||||
rra(stacks);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/14 16:55:24 by dgaillet ### ########lyon.fr */
|
||||
/* Updated: 2025/12/15 14:34:45 by dgaillet ### ########lyon.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -58,6 +58,8 @@ 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);
|
||||
int r_to_lowest(t_stack *stack, int len);
|
||||
void sort_three_a(t_stacks *stacks);
|
||||
|
||||
/*ALGORITHMS*/
|
||||
void bubble_alg(t_stacks *stacks);
|
||||
|
||||
Reference in New Issue
Block a user