-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.c
101 lines (94 loc) · 2.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alouzizi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/24 13:52:50 by alouzizi #+# #+# */
/* Updated: 2022/03/17 17:16:51 by alouzizi ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void sort2(t_node **top)
{
if ((*top)->next == NULL)
exit(2);
if ((*top)->data > (*top)->next->data)
swap(&*top, 'a');
else
exit(2);
}
void sort3(t_node **top)
{
if ((*top)->data < (*top)->next->data
&& (*top)->next->data > (*top)->next->next->data)
{
if ((*top)->data < (*top)->next->next->data)
{
reverse_rotate(&*top, 'a');
swap(&*top, 'a');
}
else
reverse_rotate(&*top, 'a');
}
if ((*top)->data > (*top)->next->data
&& (*top)->next->data < (*top)->next->next->data)
{
if ((*top)->data > (*top)->next->next->data)
rotate(&*top, 'a');
else
swap(&*top, 'a');
}
if ((*top)->data > (*top)->next->data
&& (*top)->data > (*top)->next->next->data)
{
swap(&*top, 'a');
reverse_rotate(&*top, 'a');
}
}
void sort5(t_node **stack_a, t_node **stack_b)
{
t_node *temp;
int i;
i = 0;
temp = *stack_a;
while (temp->next)
{
temp = temp->next;
i++;
}
if (i == 4)
min_push(&*stack_a, &*stack_b);
min_push(&*stack_a, &*stack_b);
sort3(&*stack_a);
if (i == 4)
top_totop(&*stack_b, &*stack_a, 'a');
top_totop(&*stack_b, &*stack_a, 'a');
}
void sort_all(t_node **a, t_node **b)
{
int *index;
int *s;
int *data;
int i;
int l;
s = list_to_tab(&*a);
i = lent(&*a);
l = 0;
index = long_subscoince(s, i, &l);
get_subscoinc_data(index, l, s, &data);
l = sizeof(data) / sizeof(int);
while (i-- > 0)
{
if (check_data((*a)->data, data, l) == 0)
rotate(&*a, 'a');
if (check_data((*a)->data, data, l) == 1)
top_totop(&*a, &*b, 'b');
}
free(data);
while (*b)
start_sorting(&*a, &*b);
i = index_min(&*a);
smart_push(&*a, &*b, i, 'a');
}