if i am passing start and end as a parameter then the program is not working
like start = 0,end = 5;
program:
///search in a rotated sorted array
#include
using namespace std;
int binarysearch(int arr[],int start,int ends,int key)
{
if(start>ends)
return -1;
int mid = (start + ends)/2;
if(key == arr[mid])///case1: if mid element is the key
{
return mid;
}
if(arr[start] <= arr[mid]) /// case2: if key lies in left part
{
if(key >= arr[start] && key <= arr[mid])
return binarysearch(arr,start,mid-1,key);
}
if(key>=arr[mid]&&key<=arr[ends]) ///case3:if key lies in right part
return binarysearch(arr,mid+1,ends,key);
return binarysearch(arr,start,mid-1,key); ///it is the transition point of rotation
}
int main()
{
int arr[] = {6,7,8,1,2,3,4,5};
int start = 0;
int ends = sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<ends;i++)
cout<<arr[i]<<' ';
cout<<endl;
int key;
cin>>key;
int res = binarysearch(arr,start,ends,key);
if(res!=-1)
cout<<"key "<<key<<" found at index "<<(res+1)<<endl;
else
cout<<"key not found";
return 0;
}