-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_utils.c
More file actions
87 lines (79 loc) · 1.93 KB
/
sort_utils.c
File metadata and controls
87 lines (79 loc) · 1.93 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ctacconi <ctacconi@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 18:56:38 by ctacconi #+# #+# */
/* Updated: 2024/04/25 16:20:01 by ctacconi ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int search_best_rotation_dir(int idx, t_stack *a)
{
int i;
t_element *tmp;
i = 1;
tmp = a->first;
while (tmp->index != idx)
{
tmp = tmp->next;
i++;
}
if (i <= (a->len / 2))
return (1);
else
return (2);
}
void move_index_x_to_top_of_b(int idx, t_stack *b, t_stack *a)
{
int direction;
direction = search_best_rotation_dir(idx, b);
if (direction == 1)
{
while (b->first->index != idx)
{
if (b->first->index == idx - 1)
pa(b, a);
else
rb(b);
}
}
else
{
while (b->first->index != idx)
{
if (b->first->index == idx - 1)
pa(b, a);
else
rrb(b);
}
}
return ;
}
void move_index_x_to_top_of_a(int idx, t_stack *a)
{
int direction;
direction = search_best_rotation_dir(idx, a);
if (direction == 1)
{
while (a->first->index != idx)
ra(a);
}
else
{
while (a->first->index != idx)
rra(a);
}
return ;
}
int which_chunk_part(t_stack *b, int chunk_size, int chunk_idx_limit)
{
if (b->first->index > (chunk_idx_limit - (chunk_size / 2)))
{
return (1);
}
else
return (0);
}