-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress_array.c
95 lines (83 loc) · 2.1 KB
/
compress_array.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
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* compress_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kakiba <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/29 13:19:58 by kakiba #+# #+# */
/* Updated: 2023/01/15 20:26:03 by kakiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
void compress_list(int **list, int n);
static int *dup_int_array(int *list, int n);
static void exec_bubble_sort(int *array, int n);
static void exec_compress(int **nsort, int *sort, int n);
static void mini_swap(int *a, int *b);
void compress_list(int **list, int n)
{
int *dup;
dup = dup_int_array(*list, n);
exec_bubble_sort(dup, n);
exec_compress(list, dup, n);
free (dup);
}
static int *dup_int_array(int *list, int n)
{
int *dup;
int i;
dup = malloc(sizeof(int) * n);
if (dup == NULL)
exit_error(NULL, list, NULL, "ERROR");
i = 0;
while (i < n)
{
dup[i] = list[i];
++i;
}
return (dup);
}
static void exec_bubble_sort(int *array, int n)
{
int i;
int j;
i = 0;
while (i < n)
{
j = 0;
while (j < n - i - 1)
{
if (array[j] > array[j + 1])
mini_swap(&array[j], &array[j + 1]);
++j;
}
++i;
}
}
void exec_compress(int **nsort, int *sort, int n)
{
int *buf;
int i;
int j;
buf = malloc(sizeof(int) * n);
if (buf == NULL)
exit_error(NULL, *nsort, sort, "ERROR");
i = 0;
while (i < n)
{
j = 0;
while ((*nsort)[i] != sort[j])
j++;
buf[i] = j;
i++;
}
free (*nsort);
*nsort = buf;
}
static void mini_swap(int *a, int *b)
{
const int c = *b;
*b = *a;
*a = c;
}