Get Email Subscription

Enter your email address:

Delivered by FeedBurner

Wednesday, April 21, 2010

This program is used for Insertion sort. (C++)


 
#include< iostream.h>
#include< conio.h>
 
int key;
int insert(int*);
void display(int*, int);
int insert(int list[])
{
int i=0;
int j,k;
cout< < "element to be inserted break condition is -0 : ";
cin>>key;
cout< < "Selected element is "< < key;
while(key!=-0)
{
k=i-1;
while((key< list[k] &&(k>=0)))
{
list[k+1]=list[k];
--k;
}
 
 
 
list[k+1]=key;
cout< < "List after inserting an element";
for(j=0;j< =i;j++)
cout< <" "< < list[j];
cout< <" \nElement to be inserted break condition is -0 ::";
cin>>key;
cout< <"\nSelected Element is : "< < key;
i++;
}
return i;
}
void display(int list[],int n)
{
int j;
cout< < "\n Fianl sorted list is as follows :\n";
for(j=0;j< n;j++)
cout< < " "< < list[j];
}
void main()
{
clrscr();
int list[200];
int n;
n=insert(list);
display(list,n);
}
 
 
 
 
--------------------------------------------------------------------------------
 
 
The output of the program ::
 
 
element to be inserted break condition is -0 : 12
Selected element is 12List after inserting an element 12
Element to be inserted break condition is -0::23
 
Selected Element is : 23List after inserting an element 12 23
Element to be inserted break condition is -0::2
 
Selected Element is : 2List after inserting an element 2 12 23
Element to be inserted break condition is -0::21
 
Selected Element is : 21List after inserting an element 2 12 21 23
Element to be inserted break condition is -0::31
 
Selected Element is : 31List after inserting an element 2 12 21 23 31
Element to be inserted break condition is -0::10
 
Selected Element is : 10List after inserting an element 2 10 12 21 23 31
Element to be inserted break condition is -0::0
 
Selected Element is : 0
Fianl sorted list is as follows :
2 10 12 21 23 31
 
 

Related Posts by Categories



0 comments:

About This Blog

Lorem Ipsum

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP