-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursively_division.c
104 lines (96 loc) · 2.09 KB
/
recursively_division.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
#include "push_swap.h"
void recursively_divide_chunk_a(t_two_stacks **stacks, int32_t chunk,
int32_t inc)
{
int32_t len;
int32_t p;
len = chunk_length((*stacks)->a, chunk);
p = len / 2;
make_index((*stacks)->a, len, stacks);
while (left_less_p_ch((*stacks)->a, chunk, p))
replace_in_b(stacks, p, chunk, inc);
len = chunk_length((*stacks)->a, chunk);
if (len <= 2)
{
if (len == 2 && (*stacks)->a->value > (*stacks)->a->next->value)
sa(*stacks);
return ;
}
recursively_divide_chunk_a(stacks, chunk, ++inc);
}
void replace_in_b(t_two_stacks **stacks, int32_t mid, int32_t chunk,
int32_t inc)
{
int32_t counter;
int32_t len;
counter = 0;
len = chunk_length((*stacks)->a, chunk);
while ((*stacks)->a->chunk == chunk && counter < len && left_less_p_ch(
(*stacks)->a, chunk, mid))
{
if ((*stacks)->a->index <= mid)
{
(*stacks)->a->chunk += inc;
pb(stacks);
continue ;
}
ra(stacks);
++counter;
}
while (counter > 0)
{
rra((*stacks));
--counter;
}
}
void replace_in_a(t_two_stacks **stacks, int32_t mid, int32_t chunk)
{
int32_t counter;
int32_t len;
counter = 0;
len = chunk_length((*stacks)->b, chunk);
while ((*stacks)->b && (*stacks)->b->chunk == chunk && counter < len
&& left_bigger_p((*stacks)->b, chunk, mid))
{
if ((*stacks)->b->index >= mid)
{
pa(stacks);
continue ;
}
rb(stacks);
++counter;
}
if (count_nodes((*stacks)->b) == chunk_length((*stacks)->b, chunk))
return ;
while (counter > 0)
{
if ((*stacks)->b->index >= mid && (*stacks)->b->chunk == chunk)
pa(stacks);
rrb((*stacks));
--counter;
}
}
void recursively_divide_chunk_b(t_two_stacks **stacks, int32_t chunk)
{
int32_t p;
int32_t len;
len = chunk_length((*stacks)->b, chunk);
p = len / 2;
make_index((*stacks)->b, len, stacks);
while (left_bigger_p((*stacks)->b, chunk, p))
replace_in_a(stacks, p, chunk);
len = chunk_length((*stacks)->b, chunk);
if (len <= 2)
{
if (len == 2)
{
if ((*stacks)->b->value < (*stacks)->b->next->value)
sb((*stacks));
pa(stacks);
pa(stacks);
}
if (len == 1)
pa(stacks);
return ;
}
}