ajout de limite dans structure de liste et fonctions list_size et find_pivot ok

This commit is contained in:
hugogogo
2021-09-25 11:48:04 +02:00
parent 8f70063f29
commit 3bb142df8e
13 changed files with 95 additions and 16 deletions

BIN
builds/algo.o Normal file

Binary file not shown.

BIN
builds/algo_bubble_sort.o Normal file

Binary file not shown.

BIN
builds/print.o Normal file

Binary file not shown.

BIN
builds/push.o Normal file

Binary file not shown.

BIN
builds/push_swap.o Normal file

Binary file not shown.

BIN
builds/reverse_rotate.o Normal file

Binary file not shown.

BIN
builds/rotate.o Normal file

Binary file not shown.

BIN
builds/stop.o Normal file

Binary file not shown.

BIN
builds/swap.o Normal file

Binary file not shown.

View File

@@ -9,6 +9,7 @@
typedef struct s_stack typedef struct s_stack
{ {
int n; int n;
int limit;
struct s_stack *next; struct s_stack *next;
} t_stack; } t_stack;

BIN
push_swap Executable file

Binary file not shown.

View File

@@ -7,28 +7,104 @@ int last_element(t_stack *a)
return (a->n); return (a->n);
} }
void recursive(t_stack **a, t_stack **b, t_list *solution, int last) // 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)
{ {
if (sublist_size(last, *a) > 5) int size;
pivot_a_to_b(a, b, solution, last);
else if (list == NULL)
return (0);
size = 1;
while (list->next != NULL && list->next->limit != 1)
{ {
sort_sublist_a(a, b, solution, last); list = list->next;
if (sublist_size(last, *b) < 5) size++;
send_sublist_to_a(a, b, solution, last);
else
pivot_b_to_a(a, b, solution, last);
} }
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);
ft_putnbrendl(list_size);
ft_putnbrendl(pivot);
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) void hugo_sort(t_stack **a, t_stack **b, t_list *solution)
{ {
(void)solution; divide_a(a, b, solution);
(void)b; /*
int last; if (sublist_size(*a) > 3)
divide_a(a, b, solution);
last = last_element(*a); else if (sublist_size(*b) > 3)
recursive(*a, *b, solution, last); {
mini_sort(a, b, solution);
divide_b(a, b, solution);
}
else
send_sublist_to_a(a, b, solution);
hugo_sort(a, b, solution);
*/
} }
// /* // /*

View File

@@ -29,9 +29,11 @@ t_stack *init_stack(int ac, char **av)
if (!start) if (!start)
ps_stop(NULL, NULL, 2); ps_stop(NULL, NULL, 2);
start->n = ft_atoi(av[ac]); start->n = ft_atoi(av[ac]);
start->limit = 0;
start->next = tmp; start->next = tmp;
tmp = start; tmp = start;
} }
start->limit = 1;
return (start); return (start);
} }
@@ -48,7 +50,7 @@ t_list *launch_algo(t_stack **a, t_stack **b, int flag)
if (flag) if (flag)
fill_solution(solution, "start"); fill_solution(solution, "start");
hugo_sort(a, b, solution); hugo_sort(a, b, solution);
//bubble_sort(a, b, solution); // bubble_sort(a, b, solution);
return (solution); return (solution);
} }