07 June, 2013

C++ Program To Perform Shell Sort.

This Post Contains A C++ Program To Perform Shell Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Shell Sort', 'Array', 'Nested Loops', 'Functions' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Perform Shell Sort.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
/* Function Declaration */
void ShellSort(int a[], int b[], int d, intx);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, dim, x, a[20], b[5];
clrscr();

/* Asking For The Input From User */
cout<<" \n Enter Dimension For Array : ";
cin>>dim;
cout<<" \n Enter "<<dim<<" Values For Array : ";
for(i=0; i<dim; i++)
{
   cin>>a[i];
}
/* Source Code For Computing Shell Sort */
cout<<" \n Enter Number Of Increments : ";
cin>>x;
cout<<" \n Enter Increments Last 1 : ";
for(i=0; i<x; i++)
{
   cin>>b[i];
}
cout<<" \n Entered Values Of Array Are : ";
for(i=0; i<dim; i++)
{
   cout<<a[i]<<"\t";
}
cout<<" \n Increments Are : ";
for(i=0; i<x; i++)
{
   cout<<b[i]<<"\t";
}

ShellSort(a, b, dim, x);         // Function Call.

getch();
}
/* End Of Main Program */

/* Function Definition */
void ShellSort(int a[], int b[], int d, intx)
{

/* Declaration Of Local Variables Of Function */
int j, k , l, y, inc, span;
for(j=0; j<x; j++)
{
   span=b[j];
   for(k=span; k<d; k++)
   {
      y=a[k];
      for(l=k-span; l>=0&&y<a[l]; l-=span)
       a[l+span]=a[l];
      a[l+span]=y;
   }
}

/* Printing The Output Onto The Screen/Console */
cout<<" \n After Sorting Array Is : ";
for(j=0; j<d; j++)
{
   cout<<a[j]<<"\t";
}
}
/* End Of Function */
Output :

Enter Dimension : 10

Enter 10 Values For Array :
91  28  73  46  50  19  82  37  64  05

Enter Number Of Increments : 3

Enter Increments Last 1 : 4  2  1

Values Of Array Are :
91  28  73  46  50  19  82  37  64  05

Increments : 4  2  1

After Sorting Array Is :
05  19  28  37  46  50  64  73  82  91




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.

Blog Archive