You are on page 1of 1

#include<iostream.

h> int A[20],n,x,i;

#include<conio.h> cout<<"\n Enter the number of elements:";

void selectionsort(int a[],int n) cin>>n;

{ cout<<"\n Enter the elements of the array:";

int pos,temp; for(int i=0;i<n;i++)

for(int i=0;i<n;i++) cin>>A[i];

{ pos=i; selectionsort(A,n);

for(int j;j<i+1;j++) cout<<"\n Array after sorting:\t";

{ if(a[pos]>a[j]) for(int i=0;i<n;i++)

{ pos=j; } cout<<A[i]<<"\t";

} cout<<"\n Enter the elements to be searched for:";

temp=a[pos]; cin>>x;

a[pos]=a[i]; bsearch(A,n,x);

a[i]=temp; } getch();

} }

void bsearch(int a[],int n,int x){

int beg=0,end=n-1,mid,pos=-1;

while((beg<=end)&&(pos==-1)){

mid=(beg+end)/2;

if(x==a[mid])

pos=mid;

else{

if(x>a[mid])

beg=mid+1;

else

end=mid-1;

}}

if(pos==-1)

cout<<"\n Element not found in list";

else

cout<<"\n Element found at position:"<<(pos+1);

void main(){

You might also like