-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_utils.c
88 lines (79 loc) · 2.07 KB
/
sort_utils.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: susajid <susajid@student.42abudhabi.ae> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/22 11:58:41 by susajid #+# #+# */
/* Updated: 2023/12/27 10:10:25 by susajid ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
bool is_sorted(t_stack *stack)
{
if (!stack)
return (true);
while (stack->next)
{
if (stack->value > stack->next->value)
return (false);
stack = stack->next;
}
return (true);
}
bool circle_sorted(t_stack *stack, int stack_len)
{
int first_val;
int min_pos;
if (!stack)
return (true);
min_pos = find_move(stack, stack_len, find_min(stack), false);
first_val = stack->value;
while (--min_pos && stack->next)
{
if (stack->value > stack->next->value)
return (false);
stack = stack->next;
}
stack = stack->next;
while (stack->next)
{
if (stack->value > stack->next->value)
return (false);
stack = stack->next;
}
if (stack->value > first_val)
return (false);
return (true);
}
long absolute(int num)
{
if (num < 0)
return ((long)num * -1);
return (num);
}
int find_min(t_stack *stack)
{
int min_value;
min_value = INT_MAX;
while (stack)
{
if (stack->value < min_value)
min_value = stack->value;
stack = stack->next;
}
return (min_value);
}
int find_max(t_stack *stack)
{
int max_value;
max_value = INT_MIN;
while (stack)
{
if (stack->value > max_value)
max_value = stack->value;
stack = stack->next;
}
return (max_value);
}