insertion sort in c++

Insertion sort 


#include<iostream.h>                                                         
#include<limits.h>                                                            
void InsSort(int[], int);     
                                               
void main()                                                                  
{                                                                            
    int ar[50], item, n, index;                                                
    cout<<"how many elements do u want to create array with?";                 
    cin>>n;                                                                    
    cout<<"\nenter array elements...";                                         
    for(int i=1; i<=n; i++)                                                    
    cin>>ar[i];                                                               
    InsSort(ar, n);                                                             
    cout<< "\n\ the sorted array is as shown below...\n";                     
    for(i=1; i<=n ;i++)                                                        
        cout<<ar[i]<<" ";                                                          
    cout<<endl;                                                                
}          
                                                                  
  void InsSort (int ar[], int size)                                            
 {  
        int tmp,j;                                                               
        ar[0] = int_min;                                                         
        for(int i=1; i<=size; i++)
    {                                                                            
         tmp=ar[i];                                                                   
         j=i-1;                                                                      
        while(tmp<ar[j])                                                             
       {  ar[j+1]=ar[j];                                                               
            j--;                                                                   
       }                                                                            
       ar[j+1]=tmp;                                                                  
       cout<<"array after pass -" << i <<" - is : ";                                
       for(int k=1; k<=size; k++)                                                    
       cout<<ar[k]<<" ";                                                            
       cout<<endl;                                                                
    }                                                                         
}                                                      

Post a Comment

1 Comments


  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

    ReplyDelete