-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102-counting_sort.c
49 lines (46 loc) · 1.19 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
#include "sort.h"
/**
* counting_sort - implement the count sort algorithm
* @array: the array of integers which are each >= 0
* @size: the size of the array
*/
void counting_sort(int *array, size_t size)
{
/* Variable declarations */
size_t i, j, k, maxVal = 0, key;
int *arraytmp, *arrayhld;
if (size < 2)
return;
/* find the maximum number */
for (i = 0; i < size; i++)
if ((size_t)array[i] > maxVal)
maxVal = array[i];
/* Allocate memory to have the size of the index 0 of the array + 1 */
arraytmp = calloc(maxVal + 1, sizeof(int));
if (arraytmp == NULL)
return;
arrayhld = calloc(size, sizeof(int));
if (arrayhld == NULL)
{
free(arraytmp);
return;
}
/* Create a loop that stores the count of each value of the array*/
for (i = 0; i < size; i++)
arraytmp[array[i]] += 1;
/* sum up each individual index with it predecessor index */
for (j = 1; j <= maxVal; j++)
arraytmp[j] += arraytmp[j - 1];
print_array(arraytmp, maxVal + 1);
/*add the value of the array as the index */
for (k = 0; k < size; k++)
{
key = array[k];
arraytmp[key] -= 1;
arrayhld[arraytmp[key]] = key;
}
for (i = 0; i < size; i++)
array[i] = arrayhld[i];
free(arraytmp);
free(arrayhld);
}