-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
136 lines (127 loc) · 2.7 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dspilleb <dspilleb@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/08 18:22:20 by dspilleb #+# #+# */
/* Updated: 2023/08/10 21:41:11 by dspilleb ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void sort_three(t_data *data)
{
int first;
int second;
int third;
first = data->stack_a->rank;
second = data->stack_a->next->rank;
third = data->stack_a->next->next->rank;
if (first > second && first < third && second < third)
sa(data);
else if (first > second && first > third && second > third)
{
sa(data);
rra(data);
}
else if (first < second && first < third && second > third)
{
sa(data);
ra(data);
}
else if (first > second && first > third && second < third)
ra(data);
else if (first < second && first > third && second > third)
rra(data);
else
return ;
}
void sort_five(t_data *data)
{
if (is_sorted(data))
return ;
pb(data);
sort_four(data);
if (data->stack_b->rank == 0)
pa(data);
else if (data->stack_b->rank == 1)
psa(data);
else if (data->stack_b->rank == 2)
{
ra(data);
pa(data);
sa(data);
rra(data);
}
else if (data->stack_b->rank == 3)
{
rra(data);
pa(data);
ra(data);
ra(data);
}
else
pra(data);
}
void sort_four(t_data *data)
{
t_node *node;
int pos;
if (is_sorted(data))
return ;
pb(data);
sort_three(data);
node = data->stack_a;
pos = 0;
while (node && node->rank < data->stack_b->rank)
{
pos++;
node = node->next;
}
if (pos == 0)
pa(data);
else if (pos == 1)
psa(data);
else
sort_four2(data, pos);
}
void sort_four2(t_data *data, int pos)
{
if (pos == 2)
{
rra(data);
pa(data);
ra(data);
ra(data);
}
else
pra(data);
}
void radix_sort(t_data *data)
{
t_node *stop;
t_node *node;
int i;
i = 0;
while (!is_sorted(data))
{
node = data->stack_a;
stop = NULL;
while (node && node->next && stop != node)
{
if ((node->rank >> (i) & 1) == 0)
pb(data);
else
{
if (!stop)
stop = node;
ra(data);
}
node = data->stack_a;
}
while (data->stack_b)
pa(data);
i ++;
}
}