#include <stdio.h>
void main(){
int i,n,key;
printf("ENTER THE NUMBER OF ELEMENTS:"); int a[n];
printf("ENTER THE ELEMENTS:"); for(i=0;i<n;i++){
}
int low=0,high=n-1;
printf("ENTER THE ELEMENT TO BE SEARCHED:"); int flag=binarysearch(a,low,high,key);
if(flag==-1)
printf("Search Unsuccessful"); else
printf("Search Successful, Element found at position :%d",flag
); }
int binarysearch(int *a,int low,int high,int key){
if(low==high)
{
if(a[low]==key)
return low;
else
return -1;
}
else
{
int mid=low+(high-low)/2;
if(key==a[mid])
return mid;
else if(key>a[mid])
return binarysearch(a,mid+1,high,key);
else
return binarysearch(a,low,mid-1,key);
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CnZvaWQgbWFpbigpewogICAgaW50IGksbixrZXk7CiAgICBwcmludGYoIjIyQTkxQTYxNTFcbiIpOwogICAgcHJpbnRmKCJFTlRFUiBUSEUgTlVNQkVSIE9GIEVMRU1FTlRTOiIpOwogICAgc2NhbmYoIiVkIiwmbik7CiAgICBpbnQgYVtuXTsKICAgIHByaW50ZigiRU5URVIgVEhFIEVMRU1FTlRTOiIpOwogICAgZm9yKGk9MDtpPG47aSsrKXsKICAgIHNjYW5mKCIlZCIsJmFbaV0pOwogICAgfQogICAgaW50IGxvdz0wLGhpZ2g9bi0xOwogICAgcHJpbnRmKCJFTlRFUiBUSEUgRUxFTUVOVCBUTyBCRSBTRUFSQ0hFRDoiKTsKICAgIHNjYW5mKCIlZCIsJmtleSk7CiAgICBpbnQgZmxhZz1iaW5hcnlzZWFyY2goYSxsb3csaGlnaCxrZXkpOwogICAgaWYoZmxhZz09LTEpCiAgICBwcmludGYoIlNlYXJjaCBVbnN1Y2Nlc3NmdWwiKTsKICAgIGVsc2UgCiAgICBwcmludGYoIlNlYXJjaCBTdWNjZXNzZnVsLCBFbGVtZW50IGZvdW5kIGF0IHBvc2l0aW9uIDolZCIsZmxhZyk7Cn0KaW50IGJpbmFyeXNlYXJjaChpbnQgKmEsaW50IGxvdyxpb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