Indented "countingSort" and fixed compilation errors

Added description about advantage of counting sort over comparison sort and
how counting sort can be stabilized.
This commit is contained in:
ubc1729 2019-11-07 18:39:38 +05:30
parent 8963e54237
commit 5c1e88cc51
1 changed files with 32 additions and 24 deletions

View File

@ -1,28 +1,36 @@
#include <stdio.h>
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]={0};
for(i=0;i<n;i++)
#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++)
b[a[i]]++; //hashing number to array index
for(i=0;i<(l+1);i++)
for(i=0;i<(l+1);i++) //unstable , stabilized by prefix sum array
{
if(b[i]>0)
{
while(b[i]!=0) //for case when number exists more than once
{
printf("%d ",i);
b[i]--;
}
}
if(b[i]>0)
{
while(b[i]!=0) //for case when number exists more than once
{
printf("%d ",i);
b[i]--;
}
}
}
return 0;
}
return 0;
}