-
Notifications
You must be signed in to change notification settings - Fork 1
/
initializer.c
101 lines (92 loc) · 2.55 KB
/
initializer.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* initializer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: msouiyeh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/01 23:06:08 by msouiyeh #+# #+# */
/* Updated: 2022/03/12 21:25:05 by msouiyeh ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int calculat_offset(int total_nums)
{
if (total_nums < 5)
return (1);
if (total_nums < 10)
return (total_nums / 5);
else if (total_nums < 150)
return (total_nums / 6);
else
return (total_nums / 13);
}
int *eleminate_top_3(int *stack, int size)
{
int *update;
int i;
update = ft_calloc(size - 3, sizeof(int));
if (!update)
exit(1);
i = -1;
while (++i < size - 3)
update[i] = stack[i];
free(stack);
return (update);
}
void sort(int *stack_a, int *sorted)
{
int i;
int j;
i = -1;
while (++i < stack_a[0])
{
j = 0;
while (++j < stack_a[0])
{
if (sorted[j - 1] > sorted[j])
{
sorted[j] ^= sorted[j - 1];
sorted[j - 1] ^= sorted[j];
sorted[j] ^= sorted[j - 1];
}
}
}
}
int *fill_and_sort(int *stack_a, int *total)
{
int i;
int *sorted;
sorted = ft_calloc(stack_a[0], sizeof(int));
if (!sorted)
exit(1);
i = -1;
while (++i < stack_a[0])
sorted[i] = stack_a[i + 1];
sort(stack_a, sorted);
if (stack_a[0] > 5)
{
sorted = eleminate_top_3(sorted, stack_a[0]);
*total = stack_a[0] - 3;
}
else
*total = stack_a[0];
return (sorted);
}
void initialize_struct(t_struct **data, int ac, char **av)
{
(*data) = ft_calloc(1, sizeof(t_struct));
if (!(*data))
exit(1);
(*data)->stack_a = fill_stack_a(ac, av, &((*data)->stack_b));
(*data)->sorted = fill_and_sort((*data)->stack_a, &((*data)->total));
(*data)->offset = calculat_offset((*data)->total);
(*data)->midle = (*data)->total / 2;
(*data)->big_val_index = (*data)->total - 1;
(*data)->start = (*data)->midle - (*data)->offset;
(*data)->end = (*data)->midle + (*data)->offset;
(*data)->down = 0;
(*data)->up = 0;
(*data)->lower = 0;
(*data)->upper = 0;
}