-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_top.c
More file actions
81 lines (74 loc) · 1.78 KB
/
push_top.c
File metadata and controls
81 lines (74 loc) · 1.78 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* push_top.c :+: :+: */
/* +:+ */
/* By: lorenasmienk <lorenasmienk@student.coda +#+ */
/* +#+ */
/* Created: 2019/12/26 13:36:05 by lorenasmien #+# #+# */
/* Updated: 2020/01/03 13:30:41 by lsmienk ######## odam.nl */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void push_back(t_setting *set, int pb)
{
int x;
x = set->a->len - pb;
if (pb > x)
{
while (x > 0)
{
rotate(set->a, 'a');
x--;
}
}
else
{
while (pb > 0)
{
rev_rotate(set->a, 'a');
pb--;
}
}
}
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);
}
int push_top(t_setting *set, int part)
{
int med;
int partition;
int len;
int pb;
set_min_max(set->a, part);
med = get_medium(set->a->min, set->a->max, part);
len = count_push_amount(set, part, med);
pb = 0;
partition = len;
while (len > 0)
{
if (set->a->stack[set->a->len - 1] < med)
{
push(set->a, set->b, 'b');
len--;
}
else
{
rotate(set->a, 'a');
pb++;
}
}
push_back(set, pb);
return (partition);
}