-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_small.c
More file actions
53 lines (49 loc) · 1.55 KB
/
sort_small.c
File metadata and controls
53 lines (49 loc) · 1.55 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_small.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: miissa <miissa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/22 10:00:00 by miissa #+# #+# */
/* Updated: 2026/02/22 10:00:00 by miissa ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void sort_three_stack(t_stack *a, t_ctx *ctx)
{
int top;
int mid;
int bot;
top = a->top->value;
mid = a->top->next->value;
bot = a->top->next->next->value;
if (top > mid && mid < bot && top < bot)
sa(a, ctx);
else if (top > mid && mid > bot)
{
sa(a, ctx);
rra(a, ctx);
}
else if (top > mid)
ra(a, ctx);
else if (top < mid && mid > bot && top < bot)
{
sa(a, ctx);
ra(a, ctx);
}
else if (top < mid && mid > bot)
rra(a, ctx);
}
void sort_small_stack(t_stack *a, t_ctx *ctx)
{
if (!a || a->size <= 1 || stack_is_sorted_asc(a))
return ;
if (a->size == 2)
{
sa(a, ctx);
return ;
}
if (a->size == 3)
sort_three_stack(a, ctx);
}