208 lines
3.5 KiB
C
208 lines
3.5 KiB
C
#include "push_swap.h"
|
|
|
|
int last_element(t_stack *a)
|
|
{
|
|
while (a->next)
|
|
a = a->next;
|
|
return (a->n);
|
|
}
|
|
|
|
// size is initialized to 1 because the loop check the next element, so it will end one step before reaching it
|
|
// it checks the next one to avoid handle the first one with ->limit set to 1
|
|
int sublist_size(t_stack *list)
|
|
{
|
|
int size;
|
|
|
|
if (list == NULL)
|
|
return (0);
|
|
size = 1;
|
|
while (list->next != NULL && list->next->limit != 1)
|
|
{
|
|
list = list->next;
|
|
size++;
|
|
}
|
|
return (size);
|
|
}
|
|
|
|
int nbr_element_smaller(t_stack *list, int size, int pivot)
|
|
{
|
|
int nbr;
|
|
|
|
nbr = 0;
|
|
while (size-- > 0)
|
|
{
|
|
if (list->n < pivot)
|
|
nbr++;
|
|
list = list->next;
|
|
}
|
|
return (nbr);
|
|
}
|
|
|
|
// pivot is the smallest of the bigger group
|
|
int find_pivot(t_stack *list, int size)
|
|
{
|
|
int pivot;
|
|
t_stack *head;
|
|
|
|
pivot = list->n;
|
|
head = list;
|
|
while (head->next && nbr_element_smaller(list, size, pivot) != size / 2)
|
|
{
|
|
head = head->next;
|
|
pivot = head->n;
|
|
}
|
|
return (pivot);
|
|
}
|
|
|
|
int divide_a(t_stack **a, t_stack **b, t_list *solution)
|
|
{
|
|
int list_size;
|
|
int pivot;
|
|
|
|
(void)b;
|
|
(void)solution;
|
|
list_size = sublist_size(*a);
|
|
pivot = find_pivot(*a, list_size);
|
|
while (list_size-- > 0)
|
|
{
|
|
if ((*a)->n >= pivot)
|
|
ra(a, &solution);
|
|
else
|
|
pb(b, a, &solution);
|
|
}
|
|
mark_step(solution);
|
|
return (0);
|
|
}
|
|
|
|
int divide_b(t_stack **a, t_stack **b, t_list *solution)
|
|
{
|
|
(void)a;
|
|
(void)b;
|
|
(void)solution;
|
|
return (0);
|
|
}
|
|
|
|
void send_sublist_to_a(t_stack **a, t_stack **b, t_list *solution)
|
|
{
|
|
(void)a;
|
|
(void)b;
|
|
(void)solution;
|
|
}
|
|
|
|
void mini_sort(t_stack **a, t_stack **b, t_list *solution)
|
|
{
|
|
(void)a;
|
|
(void)b;
|
|
(void)solution;
|
|
}
|
|
|
|
void hugo_sort(t_stack **a, t_stack **b, t_list *solution)
|
|
{
|
|
divide_a(a, b, solution);
|
|
/*
|
|
if (sublist_size(*a) > 3)
|
|
divide_a(a, b, solution);
|
|
else if (sublist_size(*b) > 3)
|
|
{
|
|
mini_sort(a, b, solution);
|
|
divide_b(a, b, solution);
|
|
}
|
|
else
|
|
send_sublist_to_a(a, b, solution);
|
|
hugo_sort(a, b, solution);
|
|
*/
|
|
}
|
|
|
|
// /*
|
|
// ** n is the nbr of elements of the list that are competiting
|
|
// ** position start at 0, ie position 3 is the 4th element
|
|
// */
|
|
// int match(t_stack *a, int n, int *smaller)
|
|
// {
|
|
// int position;
|
|
// int i;
|
|
//
|
|
// *smaller = a->n;
|
|
// position = 0;
|
|
// i = 1;
|
|
// while (a->next && i < n)
|
|
// {
|
|
// a = a->next;
|
|
// if (a->n < *smaller)
|
|
// {
|
|
// *smaller = a->n;
|
|
// position = i;
|
|
// }
|
|
// i++;
|
|
// }
|
|
// return (position);
|
|
// }
|
|
//
|
|
// void nb_to_top(t_stack **a, t_list *solution, int i)
|
|
// {
|
|
// int n;
|
|
//
|
|
// if (!i)
|
|
// return ;
|
|
// n = i;
|
|
// while (--n)
|
|
// ra(a, &solution);
|
|
// n = i;
|
|
// sa(a, &solution);
|
|
// while (--n)
|
|
// {
|
|
// rra(a, &solution);
|
|
// sa(a, &solution);
|
|
// }
|
|
// }
|
|
//
|
|
// void push_to_b(t_stack **a, t_stack **b, t_list *solution, int position)
|
|
// {
|
|
// int i;
|
|
//
|
|
// i = position;
|
|
// while (i--)
|
|
// ra(a, &solution);
|
|
// pb(b, a, &solution);
|
|
// while (++i < position)
|
|
// rra(a, &solution);
|
|
// }
|
|
//
|
|
// /*
|
|
// ** so far it's bullshit :p
|
|
// */
|
|
// void hugo_sort(t_stack **a, t_stack **b, t_list *solution)
|
|
// {
|
|
// int last;
|
|
// int position;
|
|
// int value;
|
|
//
|
|
// last = last_element(*a);
|
|
// while ((*a)->n != last)
|
|
// {
|
|
// position = match(*a, 4, &value);
|
|
// if (!(*b) || value > (*b)->n)
|
|
// push_to_b(a, b, solution, position);
|
|
// else
|
|
// ra(a, &solution);
|
|
// }
|
|
// }
|
|
/*
|
|
sa(a, &solution);
|
|
pb(b, a, &solution);
|
|
pb(b, a, &solution);
|
|
pb(b, a, &solution);
|
|
pb(b, 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);
|
|
rr(a, b, &solution);
|
|
*/
|