-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils2.c
115 lines (104 loc) · 2.26 KB
/
utils2.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
105
106
107
108
109
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alouzizi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/07 19:14:58 by alouzizi #+# #+# */
/* Updated: 2022/03/15 10:59:25 by alouzizi ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int check_data(int data, int *s, int l)
{
int i;
i = 0;
while (i < l)
{
if (s[i] == data)
return (0);
i++;
}
return (1);
}
void start_sorting(t_node **a, t_node **b)
{
t_node *tb;
int *moves;
int i;
int j;
int d;
tb = *b;
i = 0;
d = 0;
moves = malloc(sizeof(int) * lent(&*b));
while (tb)
{
j = position(tb->data, &*a);
moves[d++] = the_min(a9al_moves(&*a, &*b, j, i), 3);
i++;
tb = tb->next;
}
j = min_index(moves, lent(*&b));
d = get_data_by_index(j, &*b);
i = position(d, &*a);
d = min_index(a9al_moves(&*a, &*b, i, j), 3);
real_sorting(&*a, &*b, d, j);
}
int *a9al_moves(t_node **a, t_node **b, int ia, int ib)
{
int *s;
s = malloc(sizeof(int) * 3);
s[0] = (to_top(&*a, ia) + to_top(&*b, ib));
if (map(&*a, ia, 'h') > map(&*b, ib, 'h'))
s[1] = map(&*a, ia, 'h');
else
s[1] = map(&*b, ib, 'h');
if (map(&*a, ia, 'b') > map(&*b, ib, 'b'))
s[2] = map(&*a, ia, 'b');
else
s[2] = map(&*b, ib, 'b');
return (s);
}
int to_top(t_node **stack, int index)
{
t_node *temp;
int j;
int i;
i = 0;
j = index;
temp = *stack;
while (temp->next)
{
temp = temp->next;
i++;
}
if (i / 2 >= j)
return (j);
else
{
j = i - j + 1;
return (j);
}
}
int min_index(int *s, int l)
{
int i;
int j;
int a;
i = 0;
a = 0;
j = s[i];
while (i < l)
{
if (j > s[i])
{
j = s[i];
a = i;
}
i++;
}
free(s);
return (a);
}