-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_sorting.c
100 lines (92 loc) · 2.27 KB
/
main_sorting.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_sorting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: natamazy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/27 11:46:15 by natamazy #+# #+# */
/* Updated: 2024/02/27 11:46:17 by natamazy ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int offset_counter(int nbr)
{
int i;
int a;
int b;
i = 1;
while (i * i <= nbr)
{
if (i * i <= nbr)
a = i;
if (i * i * i <= nbr)
b = i;
i++;
}
return (a + b + 1);
}
void sorting(t_stack *a_sf, t_stack *b_sf, int counter)
{
int offset;
t_list *node;
if (!a_sf || !b_sf)
return ;
node = a_sf->start;
offset = offset_counter(a_sf->size);
while (a_sf->size != 0)
{
node = a_sf->start;
if (node->index <= counter)
{
pb(a_sf, b_sf, 1);
rb(b_sf, 1);
counter += 1;
}
else if (node->index <= counter + offset)
{
pb(a_sf, b_sf, 1);
counter += 1;
}
else
ra(a_sf, 1);
}
}
void pushing_back(t_stack *a_sf, t_stack *b_sf)
{
int index;
int position;
if (!a_sf || !b_sf)
return ;
index = (highest_node(b_sf))->index;
position = position_of_node_by_index(b_sf, index);
while (position != INT_MIN)
{
if (b_sf->start->index == index && index != INT_MIN)
{
pa(a_sf, b_sf, 1);
index -= 1;
}
else if (position > (b_sf->size / 2) && b_sf->size != 0)
rrb(b_sf, 1);
else
rb(b_sf, 1);
position = position_of_node_by_index(b_sf, index);
}
}
void full_sort(t_stack *a_sf, t_stack *b_sf)
{
if (is_stack_sorted(a_sf) == 1)
return ;
if (!a_sf || !b_sf)
return ;
if (a_sf->size <= 3)
small_sort(a_sf);
else if (a_sf->size <= 5)
sorting_5(a_sf, b_sf);
else
{
sorting(a_sf, b_sf, 0);
pushing_back(a_sf, b_sf);
}
}