-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathksort.c
81 lines (72 loc) · 1.92 KB
/
ksort.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ksort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: daxferna <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 02:26:03 by daxferna #+# #+# */
/* Updated: 2024/12/23 07:21:34 by daxferna ### ########.fr */
/* */
/* ************************************************************************** */
#include "../push_swap.h"
int ft_sqrt(int nb)
{
int i;
i = 0;
while (i * i < nb)
i++;
return (i);
}
int count_rot(t_num *stack, int index)
{
int counter;
counter = 0;
while (stack && stack->index != index)
{
stack = stack->next;
counter++;
}
return (counter);
}
void ksort_first(t_num **stack_a, t_num **stack_b, int len)
{
int i;
int k;
i = 0;
k = ft_sqrt(len) * 14 / 10;
while (*stack_a)
{
if ((*stack_a)->index <= i)
{
push(stack_b, stack_a, "pb");
rotate(stack_b, "rb");
i++;
}
else if ((*stack_a)->index <= i + k)
{
push(stack_b, stack_a, "pb");
i++;
}
else
rotate(stack_a, "ra");
}
}
void ksort_second(t_num **stack_a, t_num **stack_b, int len)
{
int rot_count;
int rrot_count;
while (len > 0)
{
rot_count = count_rot(*stack_b, len - 1);
rrot_count = (len + 3) - rot_count;
if (rot_count <= rrot_count)
while ((*stack_b)->index != len - 1)
rotate(stack_b, "rb");
else
while ((*stack_b)->index != len - 1)
rev_rotate(stack_b, "rrb");
push(stack_a, stack_b, "pa");
len--;
}
}