bubble sort binary search

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[100] , i , j ,k , l, flag=1;
cout<<”enter no. \n “;
for(i=0;i<=10;i++)
{
cin>>a[i];
}

for(i=0;i<=10;i++)
{
for(j=0;j<=9;j++)
{
if(a[j+1]<a[j])
{
k=a[j+1];
a[j+1]=a[j];
a[j]=k;
}
}
}
cout<<”\n sorted list is:”;
for(i=0;i<=10;i++)
{
cout<<a[i]<<” “;
}

cout<<”\n sorting finished…”<<”Now performing search operation using binary search method”<<endl;
cout<<”\n enter no to be searched:”;
cin>>l;
int left=0;
int right=9;
while(left<=right)
{
int mid=(left+right)/2;
if (l<a[mid])
right=mid-1;
else if(l>a[mid])
left=mid+1;
else if(l==a[mid])
{
cout<<l<<”number is at “<<mid<<”position”;
flag=0;
break;
}
}
if(flag==1)
cout<<”not found”;

}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.