Saturday, December 13, 2014

Insertion Sort Code in C++


 Other Type Of Sorting : Click here :: most important ::



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

void display(int *arr,int n)
{
 cout<<endl;
 for(int i=0;i<=n;i++){
 cout<<arr[i]<<"\t";  }
}

void insertion_sort(int *arr,int n)
{
 int pos,count,val;
 for(count=1;count<=n;count++)
 {
 val=arr[count];
 for(pos=count-1;pos>=0;pos--)
 {
 if(arr[pos]>val)     {
 cout<<"\n Swapped arr["<<pos+1<<"] with arr["<<pos<<"]\n";
 arr[pos+1]=arr[pos]; }
 else { break; };
 }
 arr[pos+1]=val;
 display(arr,6);
 }
}

void main()
{
int x[]={12,4,7,2,9,5,1};
clrscr();
display(x,6);
insertion_sort(x,6);
getch();
}


Share this

0 Comment to "Insertion Sort Code in C++"