-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_swap.c
More file actions
104 lines (95 loc) · 2.3 KB
/
Copy pathpush_swap.c
File metadata and controls
104 lines (95 loc) · 2.3 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdamouh <mdamouh@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 15:46:26 by mdamouh #+# #+# */
/* Updated: 2025/12/31 22:19:14 by mdamouh ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int get_position(t_stack *stack, int index)
{
int pos;
pos = 0;
while (stack)
{
if (stack->index == index)
return (pos);
else
pos++;
stack = stack->next;
}
return (pos);
}
void ft_buildb(t_stack **stacka, t_stack **stackb)
{
int i;
int chunk;
i = 0;
chunk = get_chunk_size(ft_lenlst(*stacka));
while (*stacka)
{
if ((*stacka)->index <= i)
{
pb(stacka, stackb);
rb(stackb);
i++;
}
else if ((*stacka)->index <= i + chunk)
{
pb(stacka, stackb);
i++;
}
else
ra(stacka);
}
}
void ft_build_fromb(t_stack **stackb, t_stack **stacka)
{
int max;
int pos;
while (*stackb)
{
max = get_max(*stackb);
if ((*stackb)->index == max)
pa(stackb, stacka);
else
{
pos = get_position(*stackb, max);
if (pos <= ft_lenlst(*stackb) / 2)
rb(stackb);
else
rrb(stackb);
}
}
}
int main(int ac, char **av)
{
t_stack *stack;
int size;
int *sortedarr;
t_stack *stackb;
stackb = NULL;
if (ac < 2)
return (0);
stack = ft_parse(ac, av);
size = ft_lenlst(stack);
if (!stack)
{
stack_frier(&stack);
return (write(2, "Error\n", 6), 0);
}
if (is_sorted(stack))
return (stack_frier(&stack), 0);
sortedarr = stack_to_sorted_array(stack, size);
indexing(&stack, sortedarr, size);
free(sortedarr);
if (size <= 5)
return (small_sort(stack, stackb, size), 0);
ft_buildb(&stack, &stackb);
ft_build_fromb(&stackb, &stack);
return (stack_frier(&stack), 0);
}