-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102-counting_sort.c
executable file
·51 lines (49 loc) · 1.03 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
#include "sort.h"
/**
* counting_sort - counting sort algo
* @array: array to sort
* @size: size of array
*/
void counting_sort(int *array, size_t size)
{
size_t i;
int j, k, num, dup;
int *counts;
if (array == NULL || size < 2)
return;
k = array[0]; /* find max num to malloc size of new array */
for (i = 1; i < size; i++)
{
if (array[i] > k)
k = array[i];
}
counts = malloc(sizeof(int) * (k + 1));
if (counts == NULL)
return;
for (j = 0; j < (k + 1); j++) /* memset counts array to 0 */
counts[j] = 0;
for (i = 0; i < size; i++) /* input counts */
{
num = array[i];
counts[num] += 1;
}
for (j = 0; j < k; j++) /* update counts array */
{
counts[j + 1] += counts[j];
}
print_array(counts, k + 1);
for (i = 0, j = 0; j < k; j++) /* replace array with sorted */
{
if ((j == 0) && counts[j] != 0)
{
for ((dup = counts[j]); dup > 0; dup--)
array[i++] = j;
}
if (counts[j + 1] > counts[j])
{
for ((dup = counts[j + 1] - counts[j]); dup > 0; dup--)
array[i++] = (j + 1);
}
}
free(counts);
}