-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_five.c
66 lines (61 loc) · 1.62 KB
/
sort_five.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_five.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mohkhald <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 01:03:11 by mohkhald #+# #+# */
/* Updated: 2025/04/05 05:04:16 by mohkhald ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int ft_min_pos(t_stack *stack)
{
int min_val;
int min_pos;
int curr_pos;
t_stack *tmp;
min_val = stack->value;
min_pos = 0;
curr_pos = 0;
tmp = stack;
while (tmp)
{
if (tmp->value < min_val)
{
min_val = tmp->value;
min_pos = curr_pos;
}
tmp = tmp->next;
curr_pos++;
}
return (min_pos);
}
void ft_sort_five(t_stack **a, t_stack **b)
{
int min_pos;
int size;
int pushed;
pushed = 0;
size = ft_stack_size(*a);
if (size <= 3)
return (ft_sort_three(a));
while (size - pushed > 3)
{
min_pos = ft_min_pos(*a);
while (min_pos > 0)
{
if (min_pos <= size / 2)
ra(a);
else
rot_rev_a(a);
min_pos = ft_min_pos(*a);
}
pb(b, a);
pushed++;
}
ft_sort_three(a);
while (*b)
pa(a, b);
}