algo fonctionne, implementation ok algo 3 et 4 pas seuls
This commit is contained in:
BIN
builds/algo.o
BIN
builds/algo.o
Binary file not shown.
Binary file not shown.
432
srcs/algo.c
432
srcs/algo.c
@@ -178,67 +178,6 @@ void mini_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
void check_front(t_stack *list, int i, int *position, int *smallest)
|
||||
{
|
||||
int j;
|
||||
|
||||
j = 1;
|
||||
while (list->next && j++ < i)
|
||||
list = list->next;
|
||||
if (list->n < *smallest)
|
||||
{
|
||||
*smallest = list->n;
|
||||
*position = i;
|
||||
}
|
||||
}
|
||||
|
||||
void check_back(t_stack *list, int i, int *position, int *smallest)
|
||||
{
|
||||
t_stack *head;
|
||||
int j;
|
||||
|
||||
head = list;
|
||||
j = 1;
|
||||
while (head != NULL)
|
||||
{
|
||||
head = head->next;
|
||||
j++;
|
||||
}
|
||||
head = list;
|
||||
j -= i;
|
||||
while (head->next && j-- >= 0)
|
||||
head = head->next;
|
||||
if (head->n < *smallest)
|
||||
{
|
||||
*smallest = head->n;
|
||||
*position = i;
|
||||
}
|
||||
}
|
||||
|
||||
int find_smallest(t_stack *list, int size)
|
||||
{
|
||||
int i;
|
||||
int front;
|
||||
int back;
|
||||
int smallest;
|
||||
int position;
|
||||
|
||||
smallest = list->n;
|
||||
i = 1;
|
||||
position = 1;
|
||||
front = sublist_size(list);
|
||||
back = size - front;
|
||||
while (++i <= front)
|
||||
check_front(list, i, &position, &smallest);
|
||||
i = 0;
|
||||
while (--i >= back)
|
||||
check_back(list, i, &position, &smallest);
|
||||
ft_printf(" / smallest:%i ", smallest);
|
||||
return (position);
|
||||
}
|
||||
*/
|
||||
|
||||
void mark_sublist(t_stack *list)
|
||||
{
|
||||
while (list && list->limit != 1)
|
||||
@@ -275,23 +214,18 @@ int find_smallest(t_stack *list)
|
||||
return (position);
|
||||
}
|
||||
|
||||
void mini_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||
void sort_big(t_stack **a, t_stack **b, t_list *solution)
|
||||
{
|
||||
int list_size;
|
||||
int next;
|
||||
int i;
|
||||
|
||||
list_size = sublist_size(*a);
|
||||
if(list_size == 1)
|
||||
return ;
|
||||
(*a)->limit = 0;
|
||||
mark_sublist(*a);
|
||||
t_stack *test;test = *a;while (test){ft_printf("%i(%i)-", test->n, test->limit);test = test->next;}
|
||||
while (--list_size >= 0)
|
||||
{
|
||||
i = 0;
|
||||
next = find_smallest(*a);
|
||||
ft_printf(" / %i", next);
|
||||
if (next > 0)
|
||||
while (++i < next)
|
||||
ra(a, &solution);
|
||||
@@ -304,18 +238,359 @@ ft_printf(" / %i", next);
|
||||
while (list_size-- > 0)
|
||||
pa(a, b, &solution);
|
||||
mark_sublist(*a);
|
||||
}
|
||||
|
||||
int find_rank(t_stack *list, int nbr, int i)
|
||||
{
|
||||
t_stack *head;
|
||||
int comp;
|
||||
int rank;
|
||||
int j;
|
||||
|
||||
head = list;
|
||||
j = 0;
|
||||
while (++j <= nbr - i)
|
||||
head = head->next;
|
||||
comp = head->n;
|
||||
head = list;
|
||||
j = 0;
|
||||
rank = 1;
|
||||
while (++j <= nbr)
|
||||
{
|
||||
if (comp > head->n)
|
||||
rank++;
|
||||
head = head->next;
|
||||
}
|
||||
return (rank);
|
||||
}
|
||||
|
||||
int order_is(t_stack *list, int nbr)
|
||||
{
|
||||
int order;
|
||||
int rank;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
order = 0;
|
||||
while (++i <= nbr)
|
||||
{
|
||||
rank = find_rank(list, nbr, i);
|
||||
order = order * 10 + rank;
|
||||
}
|
||||
return (order);
|
||||
}
|
||||
|
||||
void sort_4(t_stack **a, t_list *solution)
|
||||
{
|
||||
int order;
|
||||
|
||||
order = order_is(*a, 4);
|
||||
if (order == 1234)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 1243)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 1324)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 1342)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 1423)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 1432)
|
||||
{
|
||||
ra(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 2134)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 2143)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 2314)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 2341)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 2413)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 2431)
|
||||
{
|
||||
ra(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 3124)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 3142)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 3214)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 3241)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 3412)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 3421)
|
||||
{
|
||||
ra(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 4123)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 4132)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 4213)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 4231)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 4312)
|
||||
sa(a, &solution);
|
||||
}
|
||||
|
||||
void sort_3(t_stack **a, t_list *solution)
|
||||
{
|
||||
int order;
|
||||
|
||||
order = order_is(*a, 3);
|
||||
if (order == 123)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 132)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
sa(a, &solution);
|
||||
}
|
||||
else if (order == 213)
|
||||
{
|
||||
sa(a, &solution);
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 231)
|
||||
{
|
||||
ra(a, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
}
|
||||
else if (order == 312)
|
||||
sa(a, &solution);
|
||||
}
|
||||
|
||||
void sort_2(t_stack **a, t_list *solution)
|
||||
{
|
||||
int order;
|
||||
|
||||
order = order_is(*a, 2);
|
||||
if (order == 12)
|
||||
sa(a, &solution);
|
||||
}
|
||||
void mini_sort(t_stack **a, t_list *solution)
|
||||
{
|
||||
int list_size;
|
||||
|
||||
list_size = sublist_size(*a);
|
||||
if(list_size == 1)
|
||||
return ;
|
||||
(*a)->limit = 0;
|
||||
if(list_size == 2)
|
||||
sort_2(a, solution);
|
||||
else if(list_size == 3)
|
||||
sort_3(a, solution);
|
||||
else if(list_size == 4)
|
||||
sort_4(a, solution);
|
||||
/*
|
||||
else if(list_size == 5)
|
||||
sort_5(a, solution);
|
||||
else if(list_size == 6)
|
||||
sort_6(a, solution);
|
||||
else
|
||||
sort_big(a, b, solution);
|
||||
*/
|
||||
(*a)->limit = 1;
|
||||
mark_step(solution, "mini_sort");
|
||||
}
|
||||
|
||||
void hugo_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||
{
|
||||
if (sublist_size(*a) > 5)
|
||||
if (sublist_size(*a) > 3)
|
||||
divide_a(a, b, solution);
|
||||
else
|
||||
{
|
||||
mini_sort(a, b, solution);
|
||||
if (sublist_size(*b) > 5)
|
||||
mini_sort(a, solution);
|
||||
if (sublist_size(*b) > 3)
|
||||
divide_b(a, b, solution);
|
||||
else if (sublist_size(*b) > 0)
|
||||
send_sublist_to_a(a, b, solution);
|
||||
@@ -401,19 +676,16 @@ void hugo_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||
// }
|
||||
/*
|
||||
sa(a, &solution);
|
||||
pb(b, a, &solution);
|
||||
pb(b, a, &solution);
|
||||
pb(b, a, &solution);
|
||||
pb(b, a, &solution);
|
||||
ra(a, &solution);
|
||||
rra(a, &solution);
|
||||
|
||||
pb(b, a, &solution);
|
||||
pa(a, b, &solution);
|
||||
sa(a, &solution);
|
||||
|
||||
sb(b, &solution);
|
||||
sa(a, &solution);
|
||||
rra(a, &solution);
|
||||
rrb(b, &solution);
|
||||
rrr(a, b, &solution);
|
||||
ra(a, &solution);
|
||||
rb(b, &solution);
|
||||
rrb(b, &solution);
|
||||
|
||||
rrr(a, b, &solution);
|
||||
rr(a, b, &solution);
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user