92 lines
2.4 KiB
C
92 lines
2.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* sort_little.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/01/18 03:31:31 by apommier #+# #+# */
|
|
/* Updated: 2022/01/19 18:33:33 by apommier ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "push_swap.h"
|
|
|
|
void sort_little(t_list *list)
|
|
{
|
|
t_slist *action;
|
|
|
|
action = 0;
|
|
if (is_sorted(list))
|
|
{
|
|
ft_lstclear(&list, &free);
|
|
return ;
|
|
}
|
|
if (ft_lstsize(list) == 3)
|
|
list = sort3(list, &action);
|
|
else
|
|
list = sort5(list, &action, 0);
|
|
list = turn_list(list, action);
|
|
printf_slist(action);
|
|
s_lstclear(&action);
|
|
ft_lstclear(&list, &free);
|
|
}
|
|
|
|
t_list *sort3(t_list *a, t_slist **action)
|
|
{
|
|
if (is_sorted(a))
|
|
return (a);
|
|
if (a->index < a->next->index && a->index < a->next->next->index)
|
|
{
|
|
ft_sa_sb(&a, action, 'a', 1);
|
|
ft_ra_rb(&a, action, 'a');
|
|
}
|
|
else if (a->index > a->next->index && a->index > a->next->next->index)
|
|
{
|
|
if (a->next->index > a->next->next->index)
|
|
{
|
|
ft_sa_sb(&a, action, 'a', 1);
|
|
ft_rra_rrb(&a, action, 'a');
|
|
}
|
|
else
|
|
ft_ra_rb(&a, action, 'a');
|
|
}
|
|
else if (a->index > a->next->index && a->index < a->next->next->index)
|
|
ft_sa_sb(&a, action, 'a', 1);
|
|
else if (a->index < a->next->index && a->next->next->index)
|
|
{
|
|
ft_rra_rrb(&a, action, 'a');
|
|
}
|
|
return (a);
|
|
}
|
|
|
|
t_list *sort5(t_list *list, t_slist **action, t_list *b)
|
|
{
|
|
ft_pb(&list, &b, action);
|
|
ft_pb(&list, &b, action);
|
|
list = sort3(list, action);
|
|
if (b->index < b->next->index)
|
|
ft_sa_sb(&b, action, 'b', 1);
|
|
if (b->index == 5)
|
|
ft_pa(&list, &b, action);
|
|
while (b)
|
|
{
|
|
list = swap(list, &b, action, b);
|
|
}
|
|
return (list);
|
|
}
|
|
|
|
int is_sorted(t_list *list)
|
|
{
|
|
if (!list)
|
|
return (0);
|
|
while (list->next)
|
|
{
|
|
if (list->index > list->next->index)
|
|
return (0);
|
|
else
|
|
list = list->next;
|
|
}
|
|
return (1);
|
|
}
|