Wednesday, November 30, 2011

BINARY SEARCH CODING IN C,C++

#include<iostream>
using namespace std;
main()
{
int Arr[10]={1,2,3,4,5,6,7,8,9,10};
int i=0,j=9,mod,input;
cout<<"Type number to search ";
cin>>input;
while(i<j)
    {
       mod=(i+j)/2;
       if(input>Arr[mod])
       i=mod+1;
else
       j=mod;
    }
if(input==Arr[i])
     cout<<"\nNumber "<<input<<" is located at index "<<i<<"\n";
else
     cout<<"\nNumber "<<input<<" is not present in the list\n\n";
system("\npause");
}

No comments:

Post a Comment