forked from sisittu99/push_swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.c
98 lines (89 loc) · 2.36 KB
/
sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcerchi <mcerchi@student.42roma.it> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/26 14:49:57 by mcerchi #+# #+# */
/* Updated: 2022/02/26 14:49:58 by mcerchi ### ########.fr */
/* */
/* ************************************************************************** */
#include "./libft/libft.h"
#include "push_swap.h"
void ft_sort_three(t_list **stack_a)
{
int i;
int j;
int x;
t_list *tmp;
tmp = NULL;
i = 0;
j = 0;
x = 0;
while (!(i < j && j < x && x > i))
{
tmp = *stack_a;
i = tmp->content;
tmp = tmp->next;
j = tmp->content;
tmp = tmp->next;
x = tmp->content;
if ((i > j && j < x && x > i)
|| (i > j && j > x && x < i) || (i < j && j > x && x > i))
ft_sa(stack_a);
if (i > j && j < x && x < i)
ft_ra(stack_a);
if (i < j && j > x && x < i)
ft_rra(stack_a);
}
}
void ft_sort_five(t_list **stack_a, t_list **stack_b)
{
int i;
i = 2;
while (i-- > 0)
ft_pb(stack_a, stack_b);
ft_sort_three(stack_a);
ft_move_to_a(stack_a, stack_b);
ft_search_min(stack_a, ft_lstsize(*stack_a));
}
void ft_move_to_a(t_list **stack_a, t_list **stack_b)
{
int i;
int size_a;
int size_b;
i = 0;
size_a = ft_lstsize(*stack_a);
size_b = ft_lstsize(*stack_b);
while (*stack_b != NULL)
{
i = ft_best_nbr_b(stack_b, size_b, stack_a, size_a);
if (i < 0)
while (i++ < 0)
ft_rrb(stack_b);
else if (i > 0)
while (i-- > 0)
ft_rb(stack_b);
ft_pa(stack_b, stack_a);
size_a++;
size_b--;
}
}
void ft_move_to_b(t_list **stack_a, t_list **stack_b, int *arr, int max)
{
int i;
int size_a;
size_a = ft_lstsize(*stack_a);
i = ft_best_nbr_a(*stack_a, size_a, arr, max);
while (i != -1)
{
if (i > 0)
{
while (i-- > 0)
ft_ra(stack_a);
}
ft_pb(stack_a, stack_b);
i = ft_best_nbr_a(*stack_a, --size_a, arr, max);
}
}