-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102-counting_sort.c
63 lines (52 loc) · 1.08 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
#include "sort.h"
/**
* get_max - returns the maximum element of the array.
*
* @array: array to get the max from.
* @size: size of the array.
*
* Return: the max element of the array (int).
*/
int get_max(int *array, size_t size)
{
int max = -1;
size_t i;
for (i = 0; i < size; ++i)
{
if (array[i] > max)
max = array[i];
}
return (max);
}
/**
* counting_sort - sort using counting sort algorithm.
*
* @array: array to be sorted.
* @size: size of the array.
*/
void counting_sort(int *array, size_t size)
{
int k, *count = NULL, *output = NULL;
size_t i;
if (array == NULL || size < 2)
return;
k = get_max(array, size);
count = malloc(sizeof(size_t) * (k + 1));
output = malloc(sizeof(int) * size);
for (i = 0; i < size; ++i)
count[array[i]]++;
for (i = 1; i < (size_t) (k + 1); ++i)
count[i] += count[i - 1];
print_array(count, k + 1);
for (i = size - 1; (int) i >= 0; --i)
{
count[array[i]]--;
output[count[array[i]]] = array[i];
}
for (i = 0; i < size; ++i)
array[i] = output[i];
free(output);
output = NULL;
free(count);
count = NULL;
}