-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_small_cases.c
More file actions
110 lines (101 loc) · 2.27 KB
/
Copy pathsort_small_cases.c
File metadata and controls
110 lines (101 loc) · 2.27 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* under_five_elements.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kenakamu <kenakamu@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/14 11:48:00 by kenakamu #+# #+# */
/* Updated: 2025/08/14 12:08:45 by kenakamu ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int search_min_index(t_pushswap *ps)
{
struct t_node *tmp;
int min;
int i;
tmp = ps->stack_a.head->next;
min = ps->stack_a.head->number;
while (tmp != NULL)
{
if (min > tmp->number)
min = tmp->number;
tmp = tmp->next;
}
tmp = ps->stack_a.head;
i = 0;
while (tmp != NULL)
{
if (tmp->number == min)
break ;
tmp = tmp->next;
i++;
}
return (i);
}
void elements_are_two(t_pushswap *ps)
{
if (ps->stack_a.head->number > ps->stack_a.tail->number)
ra(ps);
}
void elements_are_three(t_pushswap *ps)
{
if (ps->stack_a.head->number > ps->stack_a.head->next->number
&& ps->stack_a.head->number > ps->stack_a.tail->number)
{
ra(ps);
}
else if (ps->stack_a.head->next->number > ps->stack_a.head->number
&& ps->stack_a.head->next->number > ps->stack_a.tail->number)
{
rra(ps);
}
if (ps->stack_a.head->number > ps->stack_a.head->next->number)
{
sa(ps);
}
}
void elements_are_four(t_pushswap *ps)
{
int i;
i = search_min_index(ps);
if (i == 1)
{
sa(ps);
}
else if (i == 2)
{
rra(ps);
rra(ps);
}
else if (i == 3)
{
rra(ps);
}
pb(ps);
elements_are_three(ps);
pa(ps);
}
void elements_are_five(t_pushswap *ps)
{
int i;
i = search_min_index(ps);
if (i == 1)
sa(ps);
else if (i == 2)
{
ra(ps);
ra(ps);
}
else if (i == 3)
{
rra(ps);
rra(ps);
}
else if (i == 4)
rra(ps);
pb(ps);
elements_are_four(ps);
pa(ps);
}