Create counting sort.c

pull/1062/head
Tyagirohan 2022-10-21 09:40:44 -05:00 committed by GitHub
parent ec8bdb7c84
commit 27f1c1a2ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,36 @@
// Counting sort is a stable Sorting Algorithm
#include<stdio.h>
void countSort(int a[],int n,int key)
{
int count[50]={0}; // assigning all elements as zero
int i,j;
for(i=0;i<n;++i)
count[a[i]]=count[a[i]]+1; // increasing value of index(elements of array given) of count array
printf("Sorted elements after applying Counting sort:");
for(i=0;i<=key;++i)
for(j=1;j<=count[i];++j)
printf("%d ",i);
}
int main()
{
int a[40],n,i,key=0;
printf("Enter number of elements:");
scanf("%d",&n);
printf("\nEnter elements in array:");
// finding the masimum elements from the array
// to declare it as size of count array
for(i=0;i<n;++i)
{
scanf("%d",&a[i]);
if(a[i]>key)
key=a[i];
}
// calling counting sort
countSort(a,n, key);
return 0;
}