-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_four.c
More file actions
90 lines (82 loc) · 1.79 KB
/
Copy pathsort_four.c
File metadata and controls
90 lines (82 loc) · 1.79 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_four.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yelallam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/24 02:43:25 by yelallam #+# #+# */
/* Updated: 2026/02/24 04:51:00 by yelallam ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int find_min(t_stack *stack, int size)
{
int min;
int count;
min = stack -> data;
count = 0;
while (count < size)
{
if (stack -> data < min)
min = stack -> data;
stack = stack -> next;
count++;
}
return (min);
}
static void do_rotation(t_stack **head, int min, int signal)
{
if (signal == 1)
{
while ((*head)->data != min)
ft_rev_rotate(head, 'a');
}
else if (signal == 0)
{
while ((*head)->data != min)
ft_rotate(head, 'a');
}
}
static void rotate_to_top(t_stack **head, int min, int size)
{
t_stack *tmp;
int count;
tmp = *head;
count = 0;
while (count < size)
{
if (tmp -> data == min)
{
if (count == 0)
break ;
else if (count > size / 2)
{
do_rotation(head, min, 1);
break ;
}
else
{
do_rotation(head, min, 0);
break ;
}
}
tmp = tmp -> next;
count++;
}
}
static void smallest_nums(t_stack **head_a, t_stack **head_b)
{
int min;
min = find_min(*head_a, 4);
rotate_to_top(head_a, min, 4);
ft_push_b(head_a, head_b);
}
void sort_four(t_stack **head_a, t_stack **head_b)
{
if (ft_lstsize(*head_a) < 4)
return ;
smallest_nums(head_a, head_b);
sort_three(head_a);
ft_push_a(head_b, head_a);
}