mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 00:41:57 +00:00
add three pre sort alg + optimized simple sort alg
This commit is contained in:
49
algorithms/utils/pre_sort.c
Normal file
49
algorithms/utils/pre_sort.c
Normal file
@@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pre_sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/15 14:21:20 by dgaillet #+# #+# */
|
||||
/* Updated: 2025/12/15 14:39:21 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 ;
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user