Skip to content

Commit

Permalink
Merge pull request #1 from ritik7jain/master
Browse files Browse the repository at this point in the history
count sort added
  • Loading branch information
harshitbansal373 authored Oct 7, 2020
2 parents bd2c03a + 7a44c09 commit 5ba3ddf
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions count_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdio.h>
#include<stdlib.h>
void swap(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
int findMax(int A[],int n)
{
int max=INT32_MIN;
int i;
for(i=0;i<n;i++)
{
if(A[i]>max)
max=A[i];
}
return max;
}
void CountSort(int A[],int n)
{
int i,j,max,*C;
max=findMax(A,n);
C=(int *)malloc(sizeof(int)*(max+1));
for(i=0;i<max+1;i++)
{
C[i]=0;
}
for(i=0;i<n;i++)
{
C[A[i]]++;
}
i=0;j=0;
while(j<max+1)
{
if(C[j]>0)
{
A[i++]=j;
C[j]--;
}
else
j++;
}
}
int main()
{
int A[]={11,13,7,12,16,9,24,5,10,3},n=10,i;
CountSort(A,n);
for(i=0;i<10;i++)
printf("%d ",A[i]);
printf("\n");
return 0;
}

0 comments on commit 5ba3ddf

Please sign in to comment.