Push_swap/sorting.c
2022-01-15 23:49:27 +01:00

172 lines
3.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sorting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/07 19:14:03 by apommier #+# #+# */
/* Updated: 2022/01/07 19:14:03 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_list *swap(t_list *a, t_list **b, s_list **action, t_list *to_swap)
{
int rotate;
int place;
t_list *start_b;
start_b = *b;
place = 0;
rotate = 0;
while ((*b)->index != to_swap->index)
{
place++;
*b = (*b)->next;
}
start_b = make_list(place, start_b, *action, 'b');
a = make_list(find_place_for_b(a, to_swap->index, ft_lstsize(start_b)), a, *action, 'a');
*b = ft_pa(&a, &start_b, action);
return (a);
}
t_list *make_list(int place, t_list *list, s_list *action, char type)
{
int lstsize;
lstsize = ft_lstsize(list);
if (place > lstsize / 2)
{
while (lstsize - place)
{
ft_rra_rrb(&list, &action, type);
place++;
}
}
else
{
while (place)
{
ft_ra_rb(&list, &action, type);
place--;
}
}
return (list);
}
t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, s_list *action)
{
int lst_size;
int size_b;
int move;
int best_move;
t_list *best_b;
int i = 0;
best_b = 0;
size_b = ft_lstsize(b);
move = size_b + ft_lstsize(a);
best_move = move;
lst_size = size_b;
while (lst_size)
{
best_move = ft_lstsize(a) + ft_lstsize(b);
b = start_b;
while (b)
{
move = find_nbr(ft_lstsize(start_b), a, start_b, b->index);
if (move < best_move)
{
best_move = move;
best_b = b;
}
b = b->next;
}
a = swap(a, &start_b, &action, best_b);
lst_size--;
}
return (a);
}
int find_place_for_b(t_list *list, int index, int size_b)
{
int count;
int save_index;
t_list *start;
start = list;
save_index = ft_lstsize(list) + size_b;
count = 0;
while (list)
{
if (list->index > index && list->index < save_index)
save_index = list->index;
list = list->next;
}
while (start->index != save_index)
{
count++;
start = start->next;
}
return (count);
}
int find_nbr(int size_b, t_list *a, t_list *b, int index_b)
{
int move;
int place;
int size_a;
size_a = ft_lstsize(a);
place = 1;
move = 0;
while (b->index != index_b)
{
place++;
b = b->next;
}
move = find_place_for_b(a, b->index, size_b);
if (move > size_a / 2)
move = size_a - move;
if (place > size_b / 2)
move += size_b - place;
else
move += place;
return (move);
}
/*void choose_swap(t_list *a, t_list *b, s_list *action, int lst_size)
{
int size_a;
int size_b;
t_list *save_b;
int move;
int save_move;
size_b = lst_size;
size_a = ft_lstsize(a);
printf("swapto a\n");
move = 0;
save_move = size_b + size_a;
while (lst_size)
{
move = find_place_for_b(a, b->index);
if (move > size_a / 2)
move = size_a - move;
if (lst_size < size_b / 2)
move += size_b - lst_size;
else
move += lst_size;
if (move < save_move)
{
save_move = move;
save_b = b;
}
b = b->next;
lst_size--;
}
find_nbr();
}*/