Tuesday, 15 December 2015

Write a program that intialize an array of 10 elements and input a integer from user to be searched...using binery search

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
   int arr[10]={10,20,30,40,50,60,70,80,90,100};
   int mid,start,end,loc,n;
   start=0;
   end=9;
   loc=-1;
   cout<<"Enter element to be search  ";
   cin>>n;
   while(start<=end)
   {
        mid=(start+end)/2;
          if(n==arr[mid])
            {
loc=mid;
            break;}
          else if(n<arr[mid])
 end=mid-1;
 else
 start=mid+1;}
 if(loc==-1)
 cout<<"Element not found ";
 else
 cout<<"Element found at "<<loc;
  getch();
}

No comments:

Post a Comment