-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_chunks_2.c
58 lines (51 loc) · 1.59 KB
/
sort_chunks_2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_chunks_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhuthmay <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/05 13:40:12 by mhuthmay #+# #+# */
/* Updated: 2024/11/05 13:42:40 by mhuthmay ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void push_chunk_to_b(t_stack *a, t_stack *b, t_bounds *bounds)
{
int pos;
pos = find_closest_in_chunk(a, bounds);
while (pos != -1)
{
optimize_push(a, b, pos);
pos = find_closest_in_chunk(a, bounds);
}
}
static void push_back_to_a(t_stack *a, t_stack *b)
{
int max;
while (b->size > 0)
{
max = get_max(b);
smart_rotate_b(b, max);
pa(a, b);
}
}
void sort_chunks(t_stack *a, t_stack *b)
{
t_bounds bounds;
int chunks;
int i;
int initial_size;
initial_size = a->size;
chunks = get_chunk_count(initial_size);
i = 0;
while (a->size > 0)
{
get_chunk_bounds(a, i, &bounds);
push_chunk_to_b(a, b, &bounds);
i++;
if (i >= chunks)
i = 0;
}
push_back_to_a(a, b);
}