Push the final version
This commit is contained in:
commit
d0fe1fb2fb
28 changed files with 1459 additions and 0 deletions
79
sources/sort_simple.c
Executable file
79
sources/sort_simple.c
Executable file
|
|
@ -0,0 +1,79 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_simple.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:07:08 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/16 20:16:06 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
void sort_4(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
int distance;
|
||||
|
||||
if (is_sorted(stack_a))
|
||||
return ;
|
||||
distance = index_distance_head(stack_a, get_min(stack_a, -1));
|
||||
if (distance == 1)
|
||||
ra(stack_a);
|
||||
else if (distance == 2)
|
||||
{
|
||||
ra(stack_a);
|
||||
ra(stack_a);
|
||||
}
|
||||
else if (distance == 3)
|
||||
rra(stack_a);
|
||||
if (is_sorted(stack_a))
|
||||
return ;
|
||||
pb(stack_a, stack_b);
|
||||
sort_3(stack_a);
|
||||
pa(stack_a, stack_b);
|
||||
}
|
||||
|
||||
void sort_5(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
int distance;
|
||||
|
||||
distance = index_distance_head(stack_a, get_min(stack_a, -1));
|
||||
if (distance == 1)
|
||||
ra(stack_a);
|
||||
else if (distance == 2)
|
||||
{
|
||||
ra(stack_a);
|
||||
ra(stack_a);
|
||||
}
|
||||
else if (distance == 3)
|
||||
{
|
||||
rra(stack_a);
|
||||
rra(stack_a);
|
||||
}
|
||||
else if (distance == 4)
|
||||
rra(stack_a);
|
||||
if (is_sorted(stack_a))
|
||||
return ;
|
||||
pb(stack_a, stack_b);
|
||||
sort_4(stack_a, stack_b);
|
||||
pa(stack_a, stack_b);
|
||||
}
|
||||
|
||||
void simple_sort(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = ft_lstsize(*stack_a);
|
||||
if (is_sorted(stack_a) || size == 0 || size == 1)
|
||||
return ;
|
||||
if (size == 2)
|
||||
sa(stack_a);
|
||||
else if (size == 3)
|
||||
sort_3(stack_a);
|
||||
else if (size == 4)
|
||||
sort_4(stack_a, stack_b);
|
||||
else if (size == 5)
|
||||
sort_5(stack_a, stack_b);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue