-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_b.c
75 lines (69 loc) · 2.14 KB
/
sort_b.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_b.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joandre- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/20 02:26:14 by joandre- #+# #+# */
/* Updated: 2024/04/07 18:15:11 by joandre- ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void reverse(t_stack **a, t_stack **b, int n)
{
while (index_numb(*a, n) && target_b(*b, n))
rrr(a, b);
while (index_numb (*a, n))
rra(a);
while (target_b(*b, n))
rrb(b);
pb(a, b);
}
static void rotate(t_stack **a, t_stack **b, int n)
{
while (index_numb(*a, n) && target_b(*b, n))
rr(a, b);
while (index_numb(*a, n))
ra(a);
while (target_b(*b, n))
rb(b);
pb(a, b);
}
static void rrotate(t_stack **a, t_stack **b, int n)
{
while (index_numb(*a, n))
rra(a);
while (target_b(*b, n))
rb(b);
pb(a, b);
}
static void rreverse(t_stack **a, t_stack **b, int n)
{
while (index_numb(*a, n))
ra(a);
while (target_b(*b, n))
rrb(b);
pb(a, b);
}
void sort_b(t_stack **a, t_stack **b)
{
int n;
unsigned int c;
n = cheap_a(*a, *b);
c = calc_a2b_reverse(*a, *b, n);
if (c > calc_a2b_rotate(*a, *b, n))
c = calc_a2b_rotate(*a, *b, n);
if (c > calc_a2b_rrotate(*a, *b, n))
c = calc_a2b_rrotate(*a, *b, n);
if (c > calc_a2b_rreverse(*a, *b, n))
c = calc_a2b_rreverse(*a, *b, n);
if (c == calc_a2b_reverse(*a, *b, n))
reverse(a, b, n);
else if (c == calc_a2b_rotate(*a, *b, n))
rotate(a, b, n);
else if (c == calc_a2b_rrotate(*a, *b, n))
rrotate(a, b, n);
else if (c == calc_a2b_rreverse(*a, *b, n))
rreverse(a, b, n);
}