-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_small_set.c
101 lines (93 loc) · 2.19 KB
/
sort_small_set.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_small_set.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhuthmay <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 15:20:07 by mhuthmay #+# #+# */
/* Updated: 2024/11/05 13:03:51 by mhuthmay ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void sort_small(t_stack *a, t_stack *b)
{
if (a->size == 2)
{
if (a->head->value > a->head->next->value)
sa(a);
}
else if (a->size == 3)
sort_three(a);
else if (a->size == 4)
sort_four(a, b);
else if (a->size == 5)
sort_five(a, b);
}
void sort_three(t_stack *a)
{
int first;
int second;
int third;
first = a->head->value;
second = a->head->next->value;
third = a->tail->value;
if (first > second && second < third && first < third)
sa(a);
else if (first > second && second > third)
{
sa(a);
rra(a);
}
else if (first > second && second < third && first > third)
ra(a);
else if (first < second && second > third && first < third)
{
sa(a);
ra(a);
}
else if (first < second && second > third && first > third)
rra(a);
}
void sort_four(t_stack *a, t_stack *b)
{
int min_pos;
min_pos = get_min_pos(a);
if (min_pos == 1)
sa(a);
else if (min_pos == 2)
{
ra(a);
ra(a);
}
else if (min_pos == 3)
rra(a);
if (!is_sorted(a))
{
pb(a, b);
sort_three(a);
pa(a, b);
}
}
void sort_five(t_stack *a, t_stack *b)
{
int min_pos;
while (a->size > 3)
{
min_pos = get_min_pos(a);
if (min_pos <= a->size / 2)
{
while (min_pos--)
ra(a);
}
else
{
while (min_pos++ < a->size)
rra(a);
}
pb(a, b);
}
sort_three(a);
while (b->size)
pa(a, b);
}