-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_algorithms2.c
162 lines (150 loc) · 3.54 KB
/
sorting_algorithms2.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sorting_algorithms2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hde-vos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/16 11:34:57 by hde-vos #+# #+# */
/* Updated: 2019/09/16 11:54:58 by hde-vos ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void stacka_move_count(t_stack *stack)
{
int i;
int stack_len;
i = 0;
stack_len = stacksize(stack);
while (i + 1 <= stack_len/2)
{
i++;
stack->move_count = i;
stack = stack->next;
}
if (stack_len % 2 == 1)
{
stack->move_count = i + 1;
stack = stack->next;
}
i++;
while (i > 1)
{
stack->move_count = i * -1;
stack = stack->next;
i--;
}
}
void stackb_move_count(t_stack *stack)
{
int i;
int stack_len;
i = 0;
stack_len = stacksize(stack);
while (i + 1 <= stack_len/2)
{
stack->move_count = i;
stack = stack->next;
i++;
}
if (stack_len % 2 == 1)
{
stack->move_count = i;
stack = stack->next;
}
i++;
while (i > 1)
{
i--;
stack->move_count = i * -1;
stack = stack->next;
}
}
t_stack *get_last_in_stack(t_stack *stack)
{
while (stack->next)
stack = stack->next;
return (stack);
}
void find_best_stackb_pos(t_stack *stacka, t_stack *stackb)
{
t_stack *stackb_curr;
t_stack *stackb_next;
t_stack *min;
t_stack *max;
while (stacka)
{
stackb_curr = stackb;
while (stackb_curr)
{
if (stackb_curr->next)
stackb_next = stackb_curr->next;
else
stackb_next = stackb;
min = find_min(stackb);
max = find_max(stackb);
if (stacka->weight < min->weight)
{
if (min->next)
stacka->best_pos_weight = min->next->weight;
else
stacka->best_pos_weight = stackb->weight;
}
else if (stacka->weight > max->weight)
{
if (max)
stacka->best_pos_weight = max->weight;
else
stacka->best_pos_weight = stackb->weight;
}
else if (stacka->weight < stackb_curr->weight && stacka->weight > stackb_next->weight &&
((stackb_curr->end != 1 && stackb_next->end != -1) || (stackb_curr->end != -1 && stackb_next->end != 1)))
{
stacka->best_pos_weight = (int)stackb_next->weight;
}
stackb_curr = stackb_curr->next;
}
stacka = stacka->next;
}
}
int stackb_is_sorted(t_stack *stackb)
{
int i;
i = 0;
while (stackb->next)
{
if (stackb->weight < stackb->next->weight)
i++;
if (stackb->next)
stackb = stackb->next;
else
break ;
}
if (i > 1)
{
// write(1, "sort_all algo failed.\n", 22);
return (1);
}
else
{
// write(1, "sort_all algo passed.\n", 22);
return (0);
}
}
int move_calc(t_stack *stacka, t_stack *stackb)
{
int stacka_move_count;
int stackb_move_count;
while (stacka)
{
stacka_move_count = stacka->move_count;
stackb_move_count = find_weight(stackb, stacka->best_pos_weight)->move_count;
if (stacka_move_count < 0)
stacka_move_count *= -1;
if (stackb_move_count < 0)
stackb_move_count *= -1;
stacka->total_move_count = stacka_move_count + stackb_move_count;
stacka = stacka->next;
}
return (1);
}