-
Notifications
You must be signed in to change notification settings - Fork 0
/
boundaries_finder.c
81 lines (70 loc) · 2.37 KB
/
boundaries_finder.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* boundaries_finder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: coskelet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 09:51:38 by coskelet #+# #+# */
/* Updated: 2022/03/09 09:51:44 by coskelet ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void find_boundaries(t_stacks *st)
{
long *ar;
int i;
ar = get_sort_array(st->a, st->a_size, &i);
st->median = ar[(long)(i / 2)];
st->sigma[0] = ar[(long)(i / 3)];
st->sigma[1] = ar[(long)(i - (i / 3))];
if (i >= SH_STACK)
{
st->pos_sigma2[0] = (long) (i / SH_STACK);
st->pos_sigma2[1] = (long) (i - (i / SH_STACK));
st->sigma2[0] = ar[st->pos_sigma2[0]];
st->sigma2[1] = ar[st->pos_sigma2[1]];
}
if (i <= SH_STACK)
st->shrt = 1;
free(ar);
}
void fill_short_boundaries(t_stacks *st, long *ar, size_t size)
{
st->sh_median = ar[(long) (size / 2)];
st->sh_sigma[0] = ar[(long) (size / 3)];
st->sh_sigma[1] = ar[(long) (size - (size / 3))];
if (size >= SH_STACK)
{
st->sh_sigma2[0] = ar[(long) (size / SH_STACK)];
st->sh_sigma2[1] = ar[(long) (size - (size / SH_STACK))];
} else if (size < SH_STACK && size >= 5)
{
st->sh_sigma2[0] = ar[0];
st->sh_sigma2[1] = ar[size - 1];
}
}
static void find_short_boundaries_a(t_stacks *st)
{
long *ar;
int size;
ar = get_sort_array(st->a, st->a_size, &size);
fill_short_boundaries(st, ar, size);
free(ar);
}
static void find_short_boundaries_b(t_stacks *st)
{
long *ar;
int size;
ar = get_sort_array(st->b, st->b_size, &size);
fill_short_boundaries(st, ar, size);
free(ar);
}
void find_short_boundaries(t_stacks *st)
{
if ((st->size < SH_STACK && st->size == st->a_size)
|| SH_STACK == st->a_size)
find_short_boundaries_a(st);
else if (st->size > SH_STACK && SH_STACK == st->b_size)
find_short_boundaries_b(st);
}