Saturday, May 18, 2013

Insertion sort C++ code

For in-depth understanding of Data Structure and Algorithm concepts refer :

1) INTRODUCTION TO ALGORITHMS by Coremen Introduction to Algorithms, 3rd Edition From flipkart.com
Introduction to Algorithms, 3rd Edition From amazon.com
Introduction to Algorithms from amazon.in

2) DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron - DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron from flipkart.com
Data Structures Using C and C++ (2nd Edition) from amazon.com
Data Structures Using C and C++ from amazon.in

#include<iostream.h>
#include<conio.h>
void insertion_sort(int a[10],int n)
{
int j,key,i;
for( j=1;j<n;j++)
{
 key=a[j];
 i=j-1;
 while(i>=0&&a[i]>key)
   {
     a[i+1]=a[i];
       i=i-1;
   }
   a[i+1]=key;
}
}
void main()
{

 int a[20],n;

 cout<<"enter the size of array\n";
 cin>>n;
 cout<<"enter the elements\n";
 for(int i=0;i<n;i++)
  cin>>a[i];

  insertion_sort(a,n);

 cout<<"sorted array is\n";
  for(int i=0;i<n;i++)
    cout<<a[i]<<",";
   getch();
 }

No comments:

Post a Comment