04 June, 2013

C++ Program To Perform Insertion Sort In Ascending Order.

This Post Contains A C++ Program To Perform Insertion Sort In Ascending Order. 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 'Insertion Sort' 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 Insertion Sort In Ascending Order.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, j, dim, val;
int a[20];
clrscr();

/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;
cout<<" \n Enter The Value : ";
cin>>val;

/* Source Code For Computing Insertion Sort In Ascending Order */
i=0;
while(i<dim)
{
   j=i-1;
   while(val<a[j] && j>=0)
   {
       a[j+1]=a[j];
       --j;
   }
   a[j+1]=val;
   if(i != dim-1)
   {
      cout<<" \n Enter The Value : ";
      cin>>val;
   }
}
getch();
}
/* End Of Main Program */




No comments:

Post a Comment

Subscribe To:

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

Blog Archive