-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSort.c
More file actions
46 lines (35 loc) · 691 Bytes
/
CountSort.c
File metadata and controls
46 lines (35 loc) · 691 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
#include<stdio.h>
void CountSort(int *A,int n)
{
int maxValue=A[0];
for(int i=0;i<n;i++)
maxValue = (maxValue>A[i])?(maxValue):A[i];
int B[maxValue+1];
for(int i=0;i<maxValue+1;i++)
B[i]=0;
for(int i=0;i<n;i++)
B[A[i]]++;
int C[maxValue+1];
C[0]=B[0];
for(int i=1;i<maxValue+1;i++)
C[i]= C[i-1]+B[i];
for(int i=maxValue;i>=0;i--)
{
while(C[i]){
A[C[i]-1]=i;
C[i]--;
}
}
}
int main()
{
int n ;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",(a+i));
CountSort(a,n);
for(int i=0;i<n;i++)
printf("%d ",*(a+i));
return 0;
}