-
Notifications
You must be signed in to change notification settings - Fork 56
/
count.c
60 lines (60 loc) · 961 Bytes
/
count.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
#include <stdio.h>
#include <stdlib.h>
int printarr(int *arr, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d\t", arr[i]);
}
printf("\n");
}
int maxnum(int *a, int n)
{
int max = 0;
for (int i = 0; i < n; i++)
{
if (max < a[i])
{
max = a[i];
}
}
return max;
}
void count(int *a, int n)
{
int max = maxnum(a, n), i, j;
int *b = (int *)malloc((max + 1) * sizeof(int));
for (i = 0; i < max + 1; i++)
{
b[i] = 0;
}
for (i = 0; i < n; i++)
{
b[a[i]] = b[a[i]] + 1;
}
i = 0;
j = 0;
while (i <= max)
{
if (b[i] > 0)
{
a[j] = i;
b[i] = b[i] - 1;
j++;
}
else
{
i++;
}
}
free(b);
}
int main()
{
int a[] = {9, 1, 24, 8, 78, 5, 6};
int n = 7;
printarr(a, n);
count(a, n);
printarr(a, n);
return 0;
}