Counting Sort

#include<stdio.h>

#include<conio.h>

void counting_sort(int [],int,int);

int a[100],count[100],b[100],i,k,n;

void main(){

// int a[100],i,n;

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

k=a[0];

for(i=1;i<n;i++){

if(k<a[i])

k=a[i];

}

counting_sort(a,n,k);

printf("After Sorting: ");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

}


void counting_sort(int a[],int n,int k){

count[k]=0;

for(i=0;i<n;i++)

++count[a[i]];

for(i=1;i<=k;i++)

count[i]+=count[i-1];

for(i=n-1;i>=0;i--)

b[--count[a[i]]]=a[i];

for(i=0;i<n;i++)

a[i]=b[i];



No comments:

Post a Comment