-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_adaptive.c
More file actions
79 lines (71 loc) · 2.08 KB
/
sort_adaptive.c
File metadata and controls
79 lines (71 loc) · 2.08 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_adaptive.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rmrad <rmrad@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/02 12:42:20 by miissa #+# #+# */
/* Updated: 2026/01/12 12:07:43 by rmrad ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static t_strategy pick_adaptive(double disorder)
{
if (disorder < 0.2)
return (START_LOW);
if (disorder < 0.5)
return (START_MEDIUM);
return (START_COMPLEX);
}
static int low_pass(t_stack *a, t_ctx *ctx)
{
int i;
int n;
int rotations;
if (!a || a->size <= 1 || stack_is_sorted_asc(a))
return (1);
n = a->size;
i = 0;
rotations = 0;
while (i < n)
{
if (a->top->next && a->top->index > a->top->next->index)
sa(a, ctx);
else
{
ra(a, ctx);
rotations++;
}
i++;
}
while (rotations-- > 0)
rra(a, ctx);
return (stack_is_sorted_asc(a));
}
static t_strategy run_low(t_stack *a, t_stack *b, t_ctx *ctx)
{
if (low_pass(a, ctx))
return (START_LOW);
sort_medium_using_chunk(a, b, ctx);
return (START_MEDIUM);
}
static t_strategy run_choice(t_stack *a, t_stack *b, t_ctx *ctx,
t_strategy choice)
{
if (choice == START_MEDIUM)
{
sort_medium_using_chunk(a, b, ctx);
return (START_MEDIUM);
}
sort_complex(a, b, ctx);
return (START_COMPLEX);
}
t_strategy sort_adaptive(t_stack *a, t_stack *b, t_ctx *ctx)
{
t_strategy choice;
choice = pick_adaptive(ctx->disorder);
if (choice == START_LOW)
return (run_low(a, b, ctx));
return (run_choice(a, b, ctx, choice));
}