-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path102-counting_sort.c
More file actions
53 lines (46 loc) · 848 Bytes
/
102-counting_sort.c
File metadata and controls
53 lines (46 loc) · 848 Bytes
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
#include "sort.h"
#include <stdio.h>
#include <stdlib.h>
/**
* counting_sort - Sorts using counting sort algorithm
*
* @array: The array to be sorted
* @size: Number of elements in @array
*/
void counting_sort(int *array, size_t size)
{
int *cmp_tab, max = 0, i, k = 0;
size_t j;
if (size > 1 && array)
{
for (i = 0; i < (int)size; i++)
{
if (array[i] > max)
max = array[i];
}
cmp_tab = malloc((max + 1) * sizeof(int));
if (!cmp_tab)
return;
for (i = 0; i < max + 1; i++)
cmp_tab[i] = 0;
for (j = 0; j < size; j++)
cmp_tab[array[j]]++;
j = 0;
for (i = 0; i < max + 1; i++)
{
for (j = cmp_tab[i]; j != 0; j--)
{
array[k] = i;
k++;
}
}
k = 0;
for (i = 0; i < max + 1; i++)
{
k += cmp_tab[i];
cmp_tab[i] = k;
}
print_array(cmp_tab, max + 1);
free(cmp_tab);
}
}