Data Structure Sorting Array C program

The Below program is to perform sorting of Array of the following types 1)Bubble Sort 2)Selection Sort 3)Insertion Sort and 4)Shell Sort. This is program is part of Mumbai University MCA Colleges Data Structures in C language. MCA Sem 2

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void Bubble();
void Selection();
void Insertion();
void Shell();
int i,j,k,n,a[20],temp,choice,c,last,incr;
void main()
{
          clrscr();
          while(choice!=5)
          {
printf("\n\n------------------------------ MENU FOR SORTING -------------------------------\n\n1.Selection Sort.\n\n2.Bubble Sort.\n\n3.Insertion Sort.\n\n4.Shell Sort.\n\n5.Exit.\n\nEnter your choice : ");
                   scanf("%d",&choice);
                   switch(choice)
                   {
                             case 1:
                                      Selection();
                                      break;

                             case 2:
                                      Bubble();
                                      break;

                             case 3:
                                      Insertion();
                                      break;

                             case 4:
                                      Shell();
                                      break;

                             case 5:
                                      exit(1);

                             default:
                                      printf("\n\nInvalid Choice!!!!!!Try Again");
                             }
                   }
          getch();
}
void Selection()
{
printf("\n\n------------------------------- Selection Sort --------------------------------");
          printf("\n\nEnter the size of the array : ");
          scanf("%d",&n);
          printf("\n\nEnter the %d elements : ",n);
          for(i=0;i<n;i++)
          {
                   scanf("%d",&a[i]);
          }
printf("\n\n---------------------------------Original Array---------------------------------\n\n");
          for(i=0;i<n;i++)
          {
                   printf("%d\t",a[i]);
          }
          for(i=0;i<n;i++)
          {
                   k=i;
                   for(j=i+1;j<n;j++)
                   {
                             if(a[k]>a[j])
                             {
                                      k=j;
                             }
                   }
                   if(k!=i)
                   {
                             temp=a[i];
                             a[i]=a[k];
                             a[k]=temp;
                   }
          }
printf("\n\n-------------------------------- Sorted Array ----------------------------------\n\n");
          for(i=0;i<n;i++)
          {
                   printf("%d\t",a[i]);
          }
}


void Bubble()
{
printf("\n\n--------------------------------- Bubble Sort ----------------------------------");
          printf("\n\nEnter the size of the array : ");
          scanf("%d",&n);
          printf("\n\nEnter the %d elements : ",n);
          for(i=0;i<n;i++)
          {
                   scanf("%d",&a[i]);
          }
printf("\n\n------------------------------- Original Array --------------------------------\n\n");
          for(i=0;i<n;i++)
          {
                   printf("%d\t",a[i]);
          }
          last= n-1;
          for(i=0;i<n-1;i++)
          {
                   for(j=0;j<last-i;j++)
                   {
                             if(a[j]>a[j+1])
                             {
                                      temp=a[j];
                                      a[j]=a[j+1];
                                      a[j+1]=temp;
                             }
                   }
          }
printf("\n\n-------------------------------- Sorted Array ----------------------------------\n\n");
          for(i=0;i<n;i++)
          {
                   printf("%d\t",a[i]);
          }
}

void Insertion()
{
printf("\n\n------------------------------- Insertion Sort -------------------------------\n\n");
          printf("Enter element (-0 to exit) : ");
          scanf("%d",&n);
          c=0;
          while(n!=-0)
          {
                   j=c-1;
                   while((n<a[j])&&(j>=0))
                   {
                             a[j+1]=a[j];
                             --j;
                   }
                   a[j+1]=n;
printf("\n\n-----------------------------After inserting element %d------------------------\n\n\n",n);
                   for(i=0;i<=c;i++)
                   {
                             printf("%d\t",a[i]);
                   }
          printf("\n\nEnter element (-0 to exit) : ");
          scanf("%d",&n);
          ++c;
          }
printf("\n\n-------------------------------- Sorted Array ----------------------------------\n\n");
          for(i=0;i<c;i++)
          {
                   printf("%d\t",a[i]);
          }
}

void Shell()
{
printf("\n\n--------------------------------- Shell Sort ---------------------------------");
          printf("\n\nEnter the size of the array : ");
          scanf("%d",&n);
          printf("\nEnter %d  elements : ",n);
          for(i=0;i<n;i++)
          {
                   scanf("%d",&a[i]);
          }
printf("\n\n---------------------------------Original Array---------------------------------\n\n");
          for (i = 0; i < n; i++)
          printf("%d\t ", a[i]);
printf("\n--------------------------------------------------------------------------------");
          incr=floor(n/2);
          while(incr>=1)
          {
                   for(j=incr;j<n;j++)
                   {
                             k=a[j];
                             for(i = j-incr; i >= 0 && k < a[i]; i = i-incr)
                             a[i+incr]=a[i];
                             a[i+incr]=k;
                   }
                   printf("\nIncrement= %d \n\n",incr);
                   for (i = 0; i < n; i++)
                   printf("%d \t", a[i]);
                   printf("\n");
                   incr=floor(incr/2);
          }
printf("\n\n---------------------------------Sorted Array-----------------------------------\n\n");
          for (i = 0; i < n; i++)
          printf("%d\t ", a[i]);
          printf("\n");
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a comment