class SearchImp
{
private int arr[];
public SearchImp(int arr[])
{
this.arr=arr;
}
public static void main(String args[])
{
int[] arr={10,20,30,40,50};
SearchImp obj=new SearchImp(arr);
int high=arr.length-1;
int searchElement=30;
System.out.println("Search element "+searchElement+" is present "+obj.iterationSearch(0,high,searchElement));
}
//Binary search of element using recursion
public boolean recursiveBinarySearch(int low,int high,int searchElement)
{
if(low>high)
{
System.out.println("null");
return false;
}
else
{
int mid=(low+high)/2;
if(searchElement==arr[mid])
{
System.out.println("Index of searchElement int array :"+mid);
return true;
}
else if(searchElement<arr[mid])
{
return recursiveBinarySearch(low,mid-1,searchElement);
}
else
{
return recursiveBinarySearch(mid+1,high,searchElement);
}
}
}
0 Comments