-
Notifications
You must be signed in to change notification settings - Fork 0
/
102-counting_sort.c
79 lines (71 loc) · 1.33 KB
/
102-counting_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
#include "sort.h"
/**
* make_counting - make a counting array
*
* @max: max value in array
* @size: size of array
* @array: array of integers
* Return: pointer to counting array
*/
int *make_counting(int max, int size, int *array)
{
int *counting;
int i;
counting = malloc(sizeof(int) * (max + 1));
if (!counting)
return (NULL);
for (i = 0; i <= max; i++)
counting[i] = 0;
i = 0;
while (i < size)
{
counting[array[i]] += 1;
i++;
}
i = 1;
while (i <= max)
{
counting[i] = counting[i] + counting[i - 1];
i++;
}
return (counting);
}
/**
* counting_sort - sort an array of integers in ascending order using the
* counting sort algorithm
* @array: array of integers
* @size: size of array
* Return: void
*/
void counting_sort(int *array, size_t size)
{
int *counting, *sorted;
int max, i, j;
if (!array || size < 2)
return;
max = array[0];
for (i = 0; i < (int)size; i++)
{
if (array[i] > max)
max = array[i];
}
counting = make_counting(max, size, array);
if (!counting)
return;
print_array(counting, max + 1);
sorted = malloc(sizeof(int) * size);
if (!sorted)
{
free(counting);
return;
}
for (i = 0; i < (int)size; i++)
{
sorted[counting[array[i]] - 1] = array[i];
counting[array[i]] -= 1;
}
for (j = 0; j < (int)size; j++)
array[j] = sorted[j];
free(counting);
free(sorted);
}