-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathother_tools2.c
111 lines (100 loc) · 2.4 KB
/
other_tools2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* other_tools2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ytaya <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/30 23:30:44 by ytaya #+# #+# */
/* Updated: 2022/02/01 16:14:54 by ytaya ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int *ft_helper(t_type **stack)
{
int *table;
t_type *head;
int i;
table = malloc(sizeof(int) * ft_lstsize(*stack));
i = 0;
head = *stack;
while (head)
{
table[i++] = head->num;
head = head->next;
}
return (ft_sortarray(table, ft_lstsize(*stack)));
}
int *ft_helperdes(t_type **stack)
{
int *table;
t_type *head;
int i;
table = malloc(sizeof(int) * ft_lstsize(*stack));
i = 0;
head = *stack;
while (head)
{
table[i++] = head->num;
head = head->next;
}
return (ft_sortarraydes(table, ft_lstsize(*stack)));
}
int ft_midpoint(int *table, int size)
{
int mid;
int nb;
int div;
if (size > 50)
div = 4;
else
div = 3;
mid = size / div;
if (mid > 0)
nb = table[mid];
else
nb = table[size / 2];
free(table);
return (nb);
}
int ft_midpoint500(int *table, int size)
{
int mid;
int nb;
int div;
div = 4;
if (size > 250)
div = 5;
else if (size < 150)
div = 3;
mid = size / div;
if (mid > 0)
nb = table[mid];
else
nb = table[size / 2];
free(table);
return (nb);
}
void ft_midalgo(t_type **stackA, t_type **stackB)
{
int *table;
int mid;
int size;
table = ft_helper(stackA);
mid = ft_midpoint(table, ft_lstsize(*stackA));
size = ft_lstsize(*stackA) / 2;
while (ft_lstsize(*stackA) != size)
{
if (!ft_chucks_finder(*stackA, mid))
return ;
if (ft_peek(stackA) < mid)
ft_push(stackA, stackB, 'B', 1);
else if (ft_lstlast(*stackA)->num < mid)
{
ft_rrotate(stackA, 'A', 1);
ft_push(stackA, stackB, 'B', 1);
}
else
ft_rotate(stackA, 'A', 1);
}
}