-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitial_devide.c
More file actions
107 lines (98 loc) · 2.53 KB
/
initial_devide.c
File metadata and controls
107 lines (98 loc) · 2.53 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
105
106
107
/* ************************************************************************** */
/* */
/* :::::::: */
/* initial_devide.c :+: :+: */
/* +:+ */
/* By: lorenasmienk <lorenasmienk@student.coda +#+ */
/* +#+ */
/* Created: 2019/12/26 19:46:14 by lorenasmien #+# #+# */
/* Updated: 2020/01/03 14:46:29 by lsmienk ######## odam.nl */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void push_part(t_setting *set, int part)
{
while (part > 0)
{
push(set->a, set->b, 'b');
part--;
}
}
static int count_push_amount(t_setting *set, int part, int med)
{
int count;
int i;
count = 0;
i = set->a->len - part;
while (i < set->a->len)
{
if (set->a->stack[i] < med)
count++;
i++;
}
return (count);
}
static int initial_push(t_setting *set, int partition)
{
int med;
int len;
med = ((set->a->min + set->a->max) / 2);
if (set->a->len > 400)
med = ((set->a->min + set->a->max) / 3.5);
len = count_push_amount(set, set->a->len, med);
while (len > 0)
{
if (set->a->stack[set->a->len - 1] < med)
{
push(set->a, set->b, 'b');
partition++;
len--;
}
else
{
rotate(set->a, 'a');
}
}
return (partition);
}
static void partition_sort(t_setting *set, int *partition, int i)
{
while (i >= 0)
{
if (is_sorted(set->b, partition[i]) && partition[i] > 0)
{
partition[i] = ret_sorted_bstack(set, partition[i]);
i--;
}
if (partition[i] == 1)
push(set->b, set->a, 'a');
if (partition[i] > 1)
partition_sort_b(set, partition[i]);
i--;
}
}
void initial_devide(t_setting *set)
{
int *partition;
int i;
partition = ft_memalloc(set->total * sizeof(int));
i = 0;
while (set->a->len > 3)
{
if (is_sorted(set->a, set->a->len))
break ;
if (is_rev_sorted(set->a, set->a->len))
{
partition[i] = set->a->len;
push_part(set, set->a->len);
partition[i] = ret_sorted_bstack(set, partition[i]);
break ;
}
set_min_max(set->a, set->a->len);
partition[i] = initial_push(set, 0);
i++;
}
small_sort(set, set->a->len);
partition_sort(set, partition, i);
free(partition);
}