You are on page 1of 3

ROLL NO: 88

AIM: to implemented binary search

//to search the element using binary search.

#include<iostream>

using namespace std;

int main()

int n,*p;

int i,num,found=0,first,last,mid;

cout<<"\n enter the size of array:";

cin>>n;

p=new int[n];

cout<<"\n Enter"<<"k elements in sorted order\n";

for(i=0;i<n;i++)

cin>>p[i];

cout<<"\n enter the element to be searched:";

cin>>num;

first=0,last=n-1;

while(last>=first&&found==0)

mid=(first+last)/2;

if(p[mid]>num)

last=mid-1;

else if(p[mid]<num)
first=mid+1;

else

if(p[mid]==num)

found=1;

if(found==0)

cout<<"number not found.";

else

cout<<"number found at "<<mid+1<<" position";

return 0;

}
OUTPUT

You might also like