-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_small_stack.c
116 lines (107 loc) · 2.6 KB
/
sort_small_stack.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_small_stack.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dvan-der <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 08:15:35 by dvan-der #+# #+# */
/* Updated: 2021/12/15 08:15:38 by dvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void sort_small_stack(t_stack **a, t_stack **b, int numbers)
{
t_stack *tmp;
tmp = (*a)->next;
if (numbers == 2)
{
if ((*a)->d_nbr > tmp->d_nbr)
execute_s(a, 'a');
}
else if (numbers == 3)
sort_3_stack(a);
else if (numbers == 4)
{
sort_4_stack(a, b, 1);
sort_3_stack(a);
execute_p(a, b, 'a');
}
else if (numbers == 5)
sort_5_stack(a, b);
}
void sort_3_stack(t_stack **a)
{
int x;
int y;
x = (*a)->next->d_nbr;
y = (*a)->next->next->d_nbr;
if ((*a)->d_nbr < x && (*a)->d_nbr < y && x > y)
{
execute_rr(a, 'a');
execute_s(a, 'a');
}
else if ((*a)->d_nbr > x && (*a)->d_nbr < y && x < y)
execute_s(a, 'a');
else if ((*a)->d_nbr < x && (*a)->d_nbr > y && x > y)
execute_rr(a, 'a');
else if ((*a)->d_nbr > x && x > y)
{
execute_r(a, 'a');
execute_s(a, 'a');
}
else if ((*a)->d_nbr > x && (*a)->d_nbr > y && y > x)
execute_r(a, 'a');
return ;
}
void sort_4_stack(t_stack **a, t_stack **b, int search)
{
int numbers;
int i;
t_stack *tmp;
tmp = *a;
numbers = 0;
while (tmp)
{
numbers++;
tmp = tmp->next;
}
i = 1;
tmp = *a;
while (tmp)
{
if (tmp->d_nbr == search)
break ;
i++;
tmp = tmp->next;
}
help_4_stack(a, b, i, numbers);
}
void help_4_stack(t_stack **a, t_stack **b, int i, int numbers)
{
if (i == 2)
execute_s(a, 'a');
else if (i == 3)
{
execute_r(a, 'a');
execute_r(a, 'a');
}
else if (i == 4 && numbers == 4)
execute_rr(a, 'a');
else if (i == 4 && numbers == 5)
{
execute_rr(a, 'a');
execute_rr(a, 'a');
}
else if (i == 5)
execute_rr(a, 'a');
execute_p(a, b, 'b');
}
void sort_5_stack(t_stack **a, t_stack **b)
{
sort_4_stack(a, b, 1);
sort_4_stack(a, b, 2);
sort_3_stack(a);
execute_p(a, b, 'a');
execute_p(a, b, 'a');
}