-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_radix_sort.c
86 lines (78 loc) · 2.37 KB
/
ft_radix_sort.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_radix_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sebasnadu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/08 23:28:52 by sebasnadu #+# #+# */
/* Updated: 2024/02/09 22:46:43 by sebasnadu ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../include/libft.h"
inline static int ft_abs(int nbr)
{
if (nbr < 0)
return (-nbr);
return (nbr);
}
static int get_max(int *nbrs, int len)
{
int i;
int max;
max = ft_abs(nbrs[0]);
i = 0;
while (++i < len)
if (ft_abs(nbrs[i]) > max)
max = ft_abs(nbrs[i]);
return (max);
}
static void counting_sort(int *nbrs, int *output, t_radix *rdata)
{
int i;
int index;
ft_memset(rdata->counter, 0, sizeof(int) * (rdata->base * 2 - 1));
i = -1;
while (++i < rdata->len)
{
index = ((nbrs[i] / rdata->expo) % rdata->base) + (rdata->base - 1);
rdata->counter[index]++;
}
i = 0;
while (++i < (rdata->base * 2) - 1)
rdata->counter[i] += rdata->counter[i - 1];
i = rdata->len;
while (--i >= 0)
{
index = ((nbrs[i] / rdata->expo) % rdata->base) + (rdata->base - 1);
output[rdata->counter[index] - 1] = nbrs[i];
rdata->counter[index]--;
}
}
void ft_radix_sort(int *nbrs, int len, int base)
{
t_radix rdata;
int i;
rdata.max = get_max(nbrs, len);
rdata.output = (int *)malloc(sizeof(int) * len);
rdata.counter = (int *)malloc(sizeof(int) * (base * 2 - 1));
if (!rdata.output || !rdata.counter)
return ;
rdata.expo = 1;
rdata.base = base;
rdata.len = len;
i = -1;
while (rdata.max / rdata.expo >= 1)
{
if (++i % 2 == 0)
counting_sort(nbrs, rdata.output, &rdata);
else
counting_sort(rdata.output, nbrs, &rdata);
rdata.expo *= base;
}
if (i % 2 == 0)
while (0 <= --rdata.len)
nbrs[rdata.len] = rdata.output[rdata.len];
free(rdata.output);
free(rdata.counter);
}