-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
127 lines (117 loc) · 3.28 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dnishsha <dnishsha@student.42berlin.d +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/10 10:26:56 by dnishsha #+# #+# */
/* Updated: 2023/07/10 10:27:01 by dnishsha ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
// This sort is applied if the stack size = 3
void small_sort(t_stack_node **stack)
{
int value_1;
int value_2;
int value_3;
if (!(*stack))
return ;
value_1 = (*stack)->value;
value_2 = (*stack)->next->value;
value_3 = (*stack)->next->next->value;
if (value_2 > value_3 && value_2 > value_1)
{
rra(stack);
if (value_3 > value_1)
sa(stack);
}
else if (value_1 > value_3 && value_1 > value_2)
{
ra(stack);
if (value_2 > value_3)
sa(stack);
}
else if (value_1 > value_2)
sa(stack);
}
// Find the node where the push_price is smallest
static t_stack_node *cheepest_node(t_stack_node *stack)
{
t_stack_node *cheepest;
if (!stack)
return (NULL);
cheepest = stack;
while (stack)
{
if (stack->push_price < cheepest->push_price)
cheepest = stack;
if (cheepest->push_price == 1)
break ;
stack = stack->next;
}
return (cheepest);
}
// Appropriate insertion from stack b
static void push_items_a(t_stack_node **a, t_stack_node **b)
{
t_stack_node *node;
while (*b)
{
node = cheepest_node(*b);
while ((node->ra)--)
ra(a);
while ((node->rb)--)
rb(b);
while ((node->rr)--)
rr(a, b);
while ((node->rra)--)
rra(a);
while ((node->rrb)--)
rrb(b);
while ((node->rrr)--)
rrr(a, b);
pa(b, a);
reset_node_data(*b);
update_target_nodes(b, *a);
}
}
// final rotation of stack a, so that the smallest no will be at the top
static void final_rotation(t_stack_node **a, size_t price_from_top,
size_t price_from_bot)
{
if (price_from_top < price_from_bot)
{
while (price_from_top--)
ra(a);
}
else
{
while (price_from_bot--)
rra(a);
}
}
// This sort is applied if the stack size > 3
void big_sort(t_stack_node **a, t_stack_node **b)
{
int stack_a_len;
t_stack_node *smallest_node;
size_t price_from_top;
size_t price_from_bot;
stack_a_len = (ft_last_node(*a)->current_pos) + 1;
while (stack_a_len > 3)
{
pb(a, b);
stack_a_len --;
}
small_sort(a);
update_target_nodes(b, *a);
push_items_a(a, b);
smallest_node = find_smallest(*a);
price_from_top = cal_price_from(smallest_node->current_pos,
(ft_last_node(*a)->current_pos) + 1, 'u');
price_from_bot = cal_price_from(smallest_node->current_pos,
(ft_last_node(*a)->current_pos) + 1, 'd');
final_rotation(a, price_from_top, price_from_bot);
}