-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
108 lines (99 loc) · 2.44 KB
/
sort.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: muraler <[email protected]. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/02 10:50:12 by muraler #+# #+# */
/* Updated: 2022/10/02 10:50:15 by muraler ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void ft_go_to_sort(t_list **list_a, t_list **list_b, int argc)
{
if (argc == 3)
sa_sb_ss(list_a, list_b, 'a');
else if (argc == 4)
sort_for_3(list_a, list_b);
else if (argc <= 6)
sort_for_5(list_a, list_b, argc);
else if (argc >= 7 && argc <= 101)
sort_for_100(list_a, list_b, argc);
else
sort_for_500(list_a, list_b, argc);
}
void sort_for_3(t_list **l1, t_list **l2)
{
int a;
int b;
int c;
a = (*l1)->data;
b = (*l1)->next->data;
c = (*l1)->next->next->data;
if (a > b && c > b && c > a)
sa_sb_ss(l1, l2, 'a');
else if (a > b && a > c && b > c)
{
sa_sb_ss(l1, l2, 'a');
rra_rrb_rrr(l1, l2, 'a');
}
else if (a > b && c > b && a > c)
ra_rb_rr(l1, l2, 'a');
else if (a < b && a < c && b > c)
{
sa_sb_ss(l1, l2, 'a');
ra_rb_rr(l1, l2, 'a');
}
else if (a < b && a > c && b > c)
rra_rrb_rrr(l1, l2, 'a');
}
int count_n(t_list *list)
{
int n;
n = 0;
while (list)
{
n++;
list = list->next;
}
return (n);
}
static void five_finish(t_list **l1, t_list **l2)
{
sort_for_3(l1, l2);
while (count_n(*l2) != 0)
{
if ((*l2)->index == 0)
pa_pb(l1, l2, 'a');
else
{
pa_pb(l1, l2, 'a');
ra_rb_rr(l1, l2, 'a');
}
}
}
void sort_for_5(t_list **l1, t_list **l2, int argc)
{
t_list *max_min;
max_min = *l1;
while (count_n(*l1) != 3)
{
if (max_min->index == 0)
{
max_min = max_min->next;
pa_pb(l1, l2, 'b');
}
else if (max_min->index == argc - 2)
{
max_min = max_min->next;
pa_pb(l1, l2, 'b');
}
else
{
max_min = max_min->next;
ra_rb_rr(l1, l2, 'a');
}
}
five_finish(l1, l2);
}