Push_swap/Utils/sort_little.c
2022-01-18 22:37:33 +01:00

110 lines
2.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_little.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/18 03:31:31 by apommier #+# #+# */
/* Updated: 2022/01/18 21:59:23 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
//t_list *end_sort3(t_list *list, t_slist **action);
//{
//}
void printf_llist(t_list *start)
{
int i;
i = 0;
printf("----printf_list----\n");
while (start)
{
i++;
printf("lst%d--nbr:%d indx: %d\n", i,*(int *)start->nbr, start->index);
start = start->next;
}
printf("----end_print----");
return ;
}
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);
}
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)
{
while (list->next)
{
if (list->index > list->next->index)
return (0);
else
list = list->next;
}
return (1);
}