Insertion, Shell, Radix Sort

#include<stdio.h>
#include<conio.h>
void insertion(int [],int);
void shell(int [],int);
void radix(int [],int);

void main()
{
 int a[100],n,i,x;

 printf("Enter number of elements of array ");
 scanf("%d",&n);
 printf("Enter the elements ");
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 printf("\nEnter \n1.Insertion\n2.Shell\n3.Radix\n\n");
 scanf("%d",&x);
 switch(x)
 {
  case 1: insertion(a,n);  break;
  case 2: shell(a,n);      break;
  case 3: radix(a,n);      break;
  default:printf("Invalid case !!\n\nUn");
 }
 printf("Sorted elements are \n");
 for(i=0;i<n;i++)
 {
  printf("%d ",a[i]);
 }

}

void insertion(int a[],int n)
{
 int m,k,t;
 for(m=1;m<n;m++)
 {
  t=a[m];
  for(k=m-1;k>=0&&a[k]>t;k--)
  {
   a[k+1]=a[k];
  }
  a[k+1]=t;
 }
}

void shell(int a[],int n)
{
 int i,j,incr,t;
 for(incr=n/2;incr>0;incr=incr/2)
 {
  for(i=incr;i<n;i++)
  {
   t=a[i];
   for(j=i-incr;j>=0&&t<a[j];j=j-incr)
   {
    a[j+incr]=a[j];
   }
   a[j+incr]=t;
  }
 }
}

void radix(int a[],int n)
{
 int b[10][10],l,p,i,j,c[10],div,k,bno;
 l=a[0];
 for(i=0;i<n;i++)
 {
  if(a[i]>l)
  {
   l=a[i];
  }
 }
 p=0;
 while(l>0)
 {
  p++;
  l=l/10;
 }
 div=1;
 for(i=1;i<=p;i++)
 {
  for(j=0;j<=9;j++)
  {
   c[j]=0;
  }
  for(j=0;j<n;j++)
  {
   bno=(a[j]/div)%10;
   b[bno][c[bno]]=a[j];
   c[bno]++;
  }
  j=0;
  for(bno=0;bno<=9;bno++)
  {
   for(k=0;k<c[bno];k++)
   {
    a[j++]=b[bno][k];
   }
  }
  div=div*10;
 }
}

/*
Output:
1.Insertion:
Enter number of elements of array 5
Enter the elements 5 4 3 2 1

Enter
1.Insertion
2.Shell
3.Radix

3
Sorted elements are
1 2 3 4 5

2.Radix:
Enter number of elements of array 5
Enter the elemetns 5 4 3 2 1

Enter
1.Insertion
2.Shell
3.Radix

3
Sorted elements are
1 2 3 4 5

3.Shell:
Enter number of elements of array 5
Enter the elemetns 5 4 3 2 1

Enter
1.Insertion
2.Shell
3.Radix

2
Sorted elements are
1 2 3 4 5
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s