Implement sorting methods using functions - bubble sort, selection sort insertion sort and shell sort C program

Saturday, 1 March 2014


void insertion_sort(int [],int);
void selection_sort(int a[],int n);
void bubble_sort(int a[],int n);
void shell_sort(int a[],int n);
void main()
{
int a[50],n,i,op;
clrscr();
do
 {
   printf("\n1)insertion\n2)Selection\n3)Bubble\n4)Shell\n5)Quit");
   printf("\nEnter your choice : ");
   scanf("%d",&op);
   switch(op)
    {
case 1: printf("\nEnter no of elements :");
scanf("%d",&n);
printf("\nEnter array elements :");
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
insertion_sort(a,n);
break;
case 2: printf("\nEnter no of elements :");
scanf("%d",&n);
printf("\nEnter array elements :");
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
selection_sort(a,n);
break;
case 3: printf("\nEnter no of elements :");
scanf("%d",&n);
printf("\nEnter array elements :");
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
bubble_sort(a,n);
break;
case 4: printf("\nEnter no of elements :");
scanf("%d",&n);
printf("\nEnter array elements :");
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
shell_sort(a,n);
break;
    }
} while(op!=5);

}

void insertion_sort(int a[],int n)
{
int i,j,temp,k;
printf("\nUnsorted Data:");
for(k=0;k<n;k++)
 printf("%5d",a[k]);

for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0 && a[j]>temp;j--)
a[j+1]=a[j];
a[j+1]=temp;
 printf("\nAfter pass   %d",i);
     for(k=0;k<n;k++)
      printf("%5d",a[k]);

}
}
void selection_sort(int a[],int n)
{
int i,j,temp,k;
printf("\nUnsorted Data:");
for(k=0;k<n;k++)
 printf("%5d",a[k]);

for(i=0;i<n-1;i++)
{       k=i;
for(j=i+1;j<n;j++)
 if(a[j]<a[k])
   k=j;
temp=a[i];
a[i]=a[k];
a[k]=temp;
 printf("\nAfter pass   %d",i+1);
     for(k=0;k<n;k++)
      printf("%5d",a[k]);

}
}
void bubble_sort(int a[],int n)
{
int i,j,k,temp;
printf("\nUnsorted Data:");
for(k=0;k<n;k++)
 printf("%5d",a[k]);
for(i=1;i<n;i++)
 {
   for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
  {
     temp=a[j];
     a[j]=a[j+1];
     a[j+1]=temp;
 }
     printf("\nAfter pass   %d",i);
     for(k=0;k<n;k++)
      printf("%5d",a[k]);
  }
}

void shell_sort(int a[],int n)
  {
     int i,j,step,pass=1,temp;
printf("\nUnsorted Data:");
for(i=0;i<n;i++)
 printf("%5d",a[i]);

     for(step=n/2; step>0 ; step=step/2)
{
  for(i=step; i< n ; i++)
     {
temp=a[i];
for(j=i; j>=step ; j=j-step)
  {
    if(temp < a[j-step])
 a[j]=a[j-step];
    else
 break;
  }
a[j]=temp;
    }
  printf("\nAfter pass   %d",pass);
  for(i=0;i<n;i++)
      printf("%5d",a[i]);
  pass++;
}
  }


OUTPUT:-

1)insertion
2)Selection
3)Bubble
4)Shell
5)Quit
Enter your choice : 1

Enter no of elements :4

Enter array elements :45 67 90 2

Unsorted Data:   45   67   90    2
After pass   1   45   67   90    2
After pass   2   45   67   90    2
After pass   3    2   45   67   90
1)insertion
2)Selection
3)Bubble
4)Shell
5)Quit
Enter your choice : 2

Enter no of elements :4

Enter array elements :34 78 5 12

Unsorted Data:   34   78    5   12
After pass   1    5   78   34   12
After pass   2    5   12   34   78
After pass   3    5   12   34   78
1)insertion
2)Selection
3)Bubble
4)Shell
5)Quit
Enter your choice : 3

Enter no of elements :4

Enter array elements :56 7 23 19

Unsorted Data:   56    7   23   19
After pass   1    7   23   19   56
After pass   2    7   19   23   56
After pass   3    7   19   23   56
1)insertion
2)Selection
3)Bubble
4)Shell
5)Quit
Enter your choice : 4

Enter no of elements :4

Enter array elements :2 67 4 8

Unsorted Data:    2   67    4    8
After pass   1    2    8    4   67
After pass   2    2    4    8   67
1)insertion
2)Selection
3)Bubble
4)Shell
5)Quit
Enter your choice : 5

2 comments

  1. Selection Sort in C

    Selection sort is simplest way to sort array elements. Selection sort is based of maximum and minimum value. First check minimum value in array list and place it at first position (position 0) of array, next find second smallest element in array list and place this value at second position (position 1) and so on. Same process is repeated until sort all element of an array.

    ReplyDelete
  2. The blog is good enough I again n again read this.
    tutorial for c++

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...
Related Posts Plugin for WordPress, Blogger...
 

Most Reading

Labels