2017-10-14 22:17:37 +08:00
|
|
|
#include <stdio.h>
|
2019-11-07 21:09:38 +08:00
|
|
|
#include<string.h>
|
|
|
|
/*
|
|
|
|
> Counting sort is a sorting technique based on keys between a specific range.
|
|
|
|
> integer sorting algorithm
|
|
|
|
> Worst-case performance O(n+k)
|
|
|
|
> Stabilized by prefix sum array
|
|
|
|
*/
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int i,n,l=0;
|
|
|
|
scanf("%d",&n);
|
|
|
|
int a[n];
|
|
|
|
for(i=0;i<n;i++)
|
|
|
|
{
|
|
|
|
scanf("%d",&a[i]);
|
|
|
|
if(a[i] > l)
|
|
|
|
l = a[i];
|
|
|
|
}
|
|
|
|
int b[l+1];
|
|
|
|
memset(b, 0, (l+1)*sizeof(b[0]));
|
|
|
|
for(i=0;i<n;i++)
|
2017-10-14 22:17:37 +08:00
|
|
|
b[a[i]]++; //hashing number to array index
|
2019-11-07 21:09:38 +08:00
|
|
|
for(i=0;i<(l+1);i++) //unstable , stabilized by prefix sum array
|
2017-10-14 22:17:37 +08:00
|
|
|
{
|
2019-11-07 21:09:38 +08:00
|
|
|
if(b[i]>0)
|
|
|
|
{
|
|
|
|
while(b[i]!=0) //for case when number exists more than once
|
|
|
|
{
|
|
|
|
printf("%d ",i);
|
|
|
|
b[i]--;
|
|
|
|
}
|
|
|
|
}
|
2017-10-14 22:17:37 +08:00
|
|
|
}
|
2019-11-07 21:09:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|