-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_sort_func.c
94 lines (85 loc) · 1.95 KB
/
helper_sort_func.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
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* helper_sort_func.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dfurneau <dfurneau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/22 20:38:33 by dfurneau #+# #+# */
/* Updated: 2021/12/11 13:57:52 by dfurneau ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/push_swap.h"
/**
* Copy Stack to a single array
* REQ FREE
**/
static int *store_array(t_stack *a)
{
int *result;
int i;
result = (int *)malloc(sizeof(int) * a->len);
if (!result)
return (NULL);
i = 0;
while (i < a->len)
{
result[i] = a->stack[i];
i++;
}
return (result);
}
/**
* Bubble Sort an array of length
* returns void an modifies the original
* array
**/
static void b_sort(int *arr, int len)
{
int i;
int j;
j = 0;
while (j < len)
{
i = 0;
while (i < len - j)
{
if (arr[i] > arr[i + 1])
ft_swapi(&arr[i], &arr[i + 1]);
i++;
}
j++;
}
}
/**
* Resturns True if the stack in the structure is
* sorted
**/
int is_sorted(t_stack *a)
{
int i;
if (a->len == 1)
return (1);
i = a->top;
while (i > 0)
{
if (a->stack[i] > a->stack[i - 1])
return (0);
i--;
}
return (1);
}
/**
* Input stack and sorts t_stack
* seperate array using Bubble Sort
* FREE REQ
**/
int *sort_array(t_stack *a)
{
int *result;
result = store_array(a);
if (!result)
return (NULL);
b_sort(result, a->top);
return (result);
}