-
Notifications
You must be signed in to change notification settings - Fork 0
/
three_sort.c
46 lines (41 loc) · 1.82 KB
/
three_sort.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* three_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marcrodr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/23 17:24:04 by marcrodr #+# #+# */
/* Updated: 2022/08/25 11:50:41 by marcrodr ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void set_values(t_stacks *stack, int *current, int *next, int *previous)
{
*current = stack->a->value;
*next = stack->a->next->value;
*previous = stack->a->previous->value;
}
void sort_three(t_stacks *stack)
{
int current;
int next;
int previous;
set_values(stack, ¤t, &next, &previous);
if ((current > next && current < previous && next < previous)
|| (current > next && current > previous && next > previous)
|| (current < next && current < previous && next > previous))
swap_a(stack);
if (ft_is_sorted(stack))
return ;
set_values(stack, ¤t, &next, &previous);
if (current < next && current > previous && next > previous)
reverse_rotate_a(stack);
if (current > next && current > previous && next < previous)
rotate_a(stack);
}
void sort_two(t_stacks *stacks)
{
if (stacks->a->value > stacks->a->next->value)
swap_a(stacks);
}