mirror of https://github.com/TheAlgorithms/C
Merge pull request #497 from ubc1729/master
Indented "counting Sort" and fixed compilation errors
This commit is contained in:
commit
70c11c370a
|
@ -1,28 +1,46 @@
|
||||||
|
/*
|
||||||
|
> 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
|
||||||
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
#include <string.h>
|
||||||
{
|
|
||||||
int i,n,l=0;
|
int main()
|
||||||
scanf("%d",&n);
|
{
|
||||||
int a[n];
|
int i, n, l = 0;
|
||||||
for(i=0;i<n;i++)
|
|
||||||
{
|
printf("Enter size of array = ");
|
||||||
scanf("%d",&a[i]);
|
scanf("%d", &n);
|
||||||
if(a[i]>l)
|
|
||||||
l=a[i];
|
int a[n];
|
||||||
}
|
printf("Enter %d elements in array :\n", n);
|
||||||
int b[l+1]={0};
|
for(i = 0; i < n; i++)
|
||||||
for(i=0;i<n;i++)
|
{
|
||||||
b[a[i]]++; //hashing number to array index
|
scanf("%d", &a[i]);
|
||||||
for(i=0;i<(l+1);i++)
|
if(a[i] > l)
|
||||||
{
|
l = a[i];
|
||||||
if(b[i]>0)
|
}
|
||||||
{
|
|
||||||
while(b[i]!=0) //for case when number exists more than once
|
int b[l + 1];
|
||||||
{
|
memset(b, 0, (l + 1) * sizeof(b[0]));
|
||||||
printf("%d ",i);
|
|
||||||
b[i]--;
|
for(i = 0; i < n; i++)
|
||||||
}
|
b[a[i]]++; //hashing number to array index
|
||||||
}
|
|
||||||
}
|
for(i = 0; i < (l + 1); i++) //unstable , stabilized by prefix sum array
|
||||||
return 0;
|
{
|
||||||
}
|
if(b[i] > 0)
|
||||||
|
{
|
||||||
|
while(b[i] != 0) //for case when number exists more than once
|
||||||
|
{
|
||||||
|
printf("%d ", i);
|
||||||
|
b[i]--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue