-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_three.c
47 lines (40 loc) · 1.52 KB
/
sort_three.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_three.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abertran <abertran@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 13:50:31 by abertran #+# #+# */
/* Updated: 2023/02/23 19:37:55 by abertran ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/* Returns the highest index in a stack. */
static int biggest_index(t_stack *stack)
{
int index;
index = stack->index;
while (stack)
{
if (stack->index > index)
index = stack->index;
stack = stack->next;
}
return (index);
}
/* Sorts a stack of 3 numbers in 2 or fewer moves. The sorting is done by index
rather than value. */
void sort_three(t_stack **stack)
{
int biggest;
if (is_sorted(*stack))
return ;
biggest = biggest_index(*stack);
if ((*stack)->index == biggest)
do_ra(stack);
else if ((*stack)->next->index == biggest)
do_rra(stack);
if ((*stack)->index > (*stack)->next->index)
do_sa(stack);
}