-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_utilities.c
61 lines (54 loc) · 1.66 KB
/
sorting_utilities.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sorting_utilities.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oadewumi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 16:45:18 by oadewumi #+# #+# */
/* Updated: 2024/05/24 17:00:54 by oadewumi ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
//This function returns a boolean, true/false if a stack is sorted or not
bool sorted(t_stack_mom *stack)
{
int i;
i = 0;
while (i < stack->top)
{
if (stack->arr[i] < stack->arr[i + 1])
return (false);
i++;
}
return (true);
}
//This function is responsible for locating the min value in a stack
int min(t_stack_mom *stack)
{
int i;
int min_val;
i = 0;
min_val = stack->arr[stack->top];
while (i <= stack->top)
{
if (stack->arr[i] < min_val)
min_val = stack->arr[i];
i++;
}
return (min_val);
}
//function to find the position of the max, min or any value in the stack
int find_position(t_stack_mom *stack, int value)
{
int pos;
int index;
pos = 0;
index = stack->top;
while (stack->arr[index] != value)
{
index--;
pos++;
}
return (pos);
}