#include<iostream.h>
#include<conio.h>
int
a1[]={2,3,1,5};
int
a2[]={5,2,1,3};
int counter,n=4;
void swap(int
i,int j)
{
counter++;
cout<<"\nswap
"<<a2[i]<<" and "<<a2n[j];
int temp=a2[i];
a2[i]=a2[j];
a2[j]=temp;
}
void main()
{
clrscr();
for(int i=0;i<n;i++)
if(a1[i]!=a2[i])
{
int pos=0;
while(a2[pos]!=a1[i])
pos++;
swap(pos,i);
}
cout<<"\n\nno of operations
required = "<<counter;
getch();
}
No comments:
Post a Comment