mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
format code
This commit is contained in:
parent
5c1e88cc51
commit
6159abcff1
@ -1,36 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.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];
|
||||
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++) //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]--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int i, n, l = 0;
|
||||
|
||||
printf("Enter size of array = ");
|
||||
scanf("%d", &n);
|
||||
|
||||
int a[n];
|
||||
printf("Enter %d elements in array :\n", 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++) //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]--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user