-
Notifications
You must be signed in to change notification settings - Fork 0
/
put_right_place.c
74 lines (67 loc) · 2.25 KB
/
put_right_place.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* put_right_place.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvieira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/28 21:36:53 by fvieira #+# #+# */
/* Updated: 2022/12/28 21:36:54 by fvieira ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
char *bottom_top_heavy(t_stack *stack, int flag)
{
int count2;
int max;
max = stack->len;
count2 = 0;
while (max--)
{
if (stack->finalpos[max] <= stack->len / 2)
count2++;
else if (stack->finalpos[max] > stack->len / 2)
count2--;
}
if (count2 >= 0 && flag == 1)
return ("rot");
else if (count2 <= 0 && flag == -1)
return ("rot");
else
return ("revrot");
}
void toomanylines(t_stack *stack_a, t_stack *stack_b, int fixed_half_len)
{
int count;
char *heavy;
heavy = bottom_top_heavy(stack_a, -1);
count = stack_a->len / 2;
while (count-- > 0)
{
while (stack_a->finalpos[0] >= fixed_half_len)
rot_or_revrot(stack_a, heavy, 'a');
put_right_place(stack_b, stack_a->finalpos[0], 'b', "to_b");
push(stack_a, stack_b, 'b');
}
biggestfirstplace(stack_b);
putbackfromb(stack_a, stack_b, fixed_half_len);
}
void sort100less(t_stack *stack_a, t_stack *stack_b)
{
int count;
int fixed_half_len;
char *heavy;
heavy = bottom_top_heavy(stack_a, 1);
count = stack_a->len / 2;
fixed_half_len = stack_a->len / 2;
while (count-- > 0)
{
while (stack_a->finalpos[0] < fixed_half_len)
rot_or_revrot(stack_a, heavy, 'a');
put_right_place(stack_b, stack_a->finalpos[0], 'b', "to_b");
push(stack_a, stack_b, 'b');
}
biggestfirstplace(stack_b);
putbackfromb(stack_a, stack_b, fixed_half_len);
toomanylines(stack_a, stack_b, fixed_half_len);
}