sk

sk

sk

#include<iostream>

using namespace std;

int binary(int arr[],int up,int lw,int item)

{

while(lw<=up)

{

  int mid = (lw+up)/2;

  if(arr[mid]==item)

  {

 return mid;

  }

  if(arr[mid]<item)

  {

 up=mid-1;

  }

  else if(arr[mid]>item)

  {

 lw=mid+1;

  }

  else

   return -1;

}

}

int main()

{

int n,item;

int arr[n],result;

cout<<"Enter the size of the array";

cin>>n;

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

{

cin>>arr[i];

}

cout<<"Enter the element that u want to find";

cin>>item;

result=binary(arr,n-1,0,item);

if(result==-1)

{

cout<<"Element not found";

}

else

{

cout<<"Element found at location :"<<result;

}

return 0;

}

Report Page