Friday, January 16, 2015

Insertion Sorting- Learn

Description:  A Program to illustrate insertion sort in an array.


Compiler:  Turbo C++ 3.0

Author:  Pranav Vats



#include<iostream.h>
#include<conio.h>
#include<limits.h>

int main()
{
clrscr();
int n,i,j,temp,a[20];

cout<<"Enter the no of elements in array : ";
cin>>n;
n+=1;
cout<<"Enter array : ";
for(i=1;i<n;i++)
 cin>>a[i];


a[0]=INT_MIN;   //ascending order

for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(temp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}

cout<<"\n\nSorted Array : ";
for(i=1;i<n;i++)
{
cout<<a[i]
<<' ';
}
getch();


 return 0;

}
    Please LIKE & SHARE US if you find this helpful!

Share

& Comment

0 comments:

Post a Comment

 

Copyright © 2015 Code Fervor™ is a registered trademark.

Designed by Templateism By Naman Kumar