-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_sort_func100.c
68 lines (63 loc) · 1.7 KB
/
helper_sort_func100.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* helper_sort_func100.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dfurneau <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/28 00:20:27 by dfurneau #+# #+# */
/* Updated: 2021/12/11 14:11:01 by dfurneau ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/push_swap.h"
/**
* Rotate stack to top based on the position as int
**/
static void rotate_search_num_top(t_stack *a, int pos)
{
int i;
i = 0;
if (pos == a->top || a->top == -1)
return ;
if (pos >= (a->len / 2))
{
while (a->top - (pos + 1) >= i++)
ra(a);
}
else
{
while (pos + 1 > i++)
rra(a);
}
}
/**
* Push Chunks of chunk_size ordered by smallest to
* largest
**/
void push_all_chunk(t_stack *a, t_stack *b, int chunk_size)
{
int *sort;
int median;
int len;
int i;
int j;
i = a->top;
while (i != -1)
{
sort = sort_array(a);
if (!sort)
error(a, b);
median = sort[(a->len - 1) / chunk_size];
j = 0;
len = a->len / chunk_size;
while (j < len)
{
rotate_search_num_top(a, int_search_stack(a, median));
pb(a, b);
j++;
}
i--;
free(sort);
sort = NULL;
}
}