-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_5.c
121 lines (112 loc) · 2.63 KB
/
sort_5.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_5.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lstorey <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/24 22:24:28 by lstorey #+# #+# */
/* Updated: 2024/03/26 16:27:40 by lstorey ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static t_ps_list *find_lowest(t_ps_list **stack_a)
{
t_ps_list *tmp;
t_ps_list *lowest;
tmp = (*stack_a);
lowest = (*stack_a);
while (tmp->next)
{
if (tmp->next->index < tmp->index)
lowest = tmp->next;
tmp = tmp->next;
}
return (lowest);
}
static int distance_to_top(t_ps_list **stack_a)
{
int distance;
t_ps_list *tmp;
distance = 0;
tmp = (*stack_a);
while (tmp)
{
if (tmp->index != 0)
distance++;
else
return (distance);
tmp = tmp->next;
}
return (distance);
}
static void first_move(t_ps_list **stack_a, t_ps_list **stack_b, int distance)
{
if (distance == 0)
pb(stack_a, stack_b);
else if (distance == 1)
{
ra(stack_a);
pb(stack_a, stack_b);
}
else if (distance == 2)
{
ra(stack_a);
ra(stack_a);
pb(stack_a, stack_b);
}
else if (distance == 3)
{
rra(stack_a);
rra(stack_a);
pb(stack_a, stack_b);
}
else
{
rra(stack_a);
pb(stack_a, stack_b);
}
}
static void second_move(t_ps_list **stack_a, t_ps_list **stack_b, int distance)
{
if (distance == 0)
pb(stack_a, stack_b);
else if (distance == 1)
{
ra(stack_a);
pb(stack_a, stack_b);
}
else if (distance == 2)
{
ra(stack_a);
ra(stack_a);
pb(stack_a, stack_b);
}
else
{
rra(stack_a);
pb(stack_a, stack_b);
}
}
void sort_5(t_ps_list **stack_a, t_ps_list **stack_b)
{
t_ps_list *lowest;
int distance;
t_ps_list *tmp;
lowest = find_lowest(stack_a);
distance = distance_to_top(stack_a);
first_move(stack_a, stack_b, distance);
lowest = find_second_lowest(stack_a);
distance = second_distance_to_top(stack_a);
second_move(stack_a, stack_b, distance);
tmp = (*stack_a);
while (tmp)
{
tmp->index = -1;
tmp = tmp->next;
}
adding_index(stack_a);
sort_3(stack_a);
pa(stack_a, stack_b);
pa(stack_a, stack_b);
}