-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_medium.c
More file actions
93 lines (83 loc) · 2.06 KB
/
sort_medium.c
File metadata and controls
93 lines (83 loc) · 2.06 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_medium.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rmrad <rmrad@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/02 12:42:24 by miissa #+# #+# */
/* Updated: 2026/01/06 11:52:33 by rmrad ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int int_sqrt(int n)
{
int i;
i = 1;
while (i * i <= n)
{
if (i * i == n)
return (i);
else if (i * i < n)
i++;
}
return (i - 1);
}
static int calculate_chunck(int n)
{
int chunck;
chunck = int_sqrt(n);
if (chunck < 1)
chunck = 1;
return (chunck);
}
static void push_to_b_window(t_stack *a, t_stack *b, t_ctx *ctx, t_chunk *c)
{
if (a->top->index <= *(c->next))
{
pb(a, b, ctx);
rb(b, ctx);
(*(c->next))++;
}
else if (a->top->index < *(c->next) + c->chunk)
{
pb(a, b, ctx);
(*(c->next))++;
}
else
ra(a, ctx);
}
static void push_max_to_a(t_stack *a, t_stack *b, t_ctx *ctx)
{
int pos;
pos = stack_pos_of_max_index(b);
if (pos <= b->size / 2)
while (pos-- > 0)
rb(b, ctx);
else
{
pos = b->size - pos;
while (pos-- > 0)
rrb(b, ctx);
}
pa(a, b, ctx);
}
void sort_medium_using_chunk(t_stack *a, t_stack *b, t_ctx *ctx)
{
int chunck;
int next;
t_chunk c;
if (a && a->size > 1 && !stack_is_sorted_asc(a))
{
chunck = calculate_chunck(a->size);
next = 0;
while (a->size > 0)
{
c.next = &next;
c.chunk = chunck;
push_to_b_window(a, b, ctx, &c);
}
while (b->size > 0)
push_max_to_a(a, b, ctx);
}
}