-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_swap.c
91 lines (82 loc) · 1.96 KB
/
push_swap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asoler <asoler@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/13 14:59:24 by asoler #+# #+# */
/* Updated: 2022/09/06 17:54:06 by asoler ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void sort_3(t_stack *a)
{
t_list *last;
long bigger;
if (a->size == 1)
return ;
bigger = a->size - 1;
last = ft_lstlast(a->lst);
if (last->content != bigger)
{
if (a->lst->content != bigger)
reverse_rotate(a);
else
rotate(a);
}
if (a->lst->content > a->lst->next->content)
swap(a);
}
void sort_n(t_stack *a, t_stack *b)
{
long shift;
long count;
shift = 0;
while (!is_sorted(a->lst))
{
count = 0;
while (count < a->size)
{
if ((a->lst->content >> shift) & 1)
rotate(a);
else
push(a, b);
count++;
}
while (b->lst)
push(b, a);
shift++;
}
}
t_stack sort(t_stack *a)
{
t_stack b;
b.lst = NULL;
b.type = 'b';
if (is_sorted(a->lst))
return (b);
if (a->size <= 5)
{
while (ft_lstsize(a->lst) > 3)
push_smallers(a, &b);
sort_3(a);
while (b.lst)
push(&b, a);
}
else
sort_n(a, &b);
return (b);
}
int main(int argc, char *argv[])
{
t_stack a;
if (argc == 1)
exit (EXIT_FAILURE);
init_struct(&a, argc);
verify_arg_rules(argv, &a);
normalize(&a);
sort(&a);
free_list(a.lst);
exit (EXIT_SUCCESS);
}