Assume that the given list has been sorted in ascending orde
Assume that the given list has been sorted in ascending order. Devise a recursive (Divide and Conquer) \"midterm\' search algorithm which first tests the element at position n/7 for equality with some value x, and then possibly checks the element at position 4n/7. The result is either discovering x or reducing the set size to a fraction of the original.
Solution
We have to perform a binary search algorithm:
public class binary search
{
public int binarySearch(int[] sortedarray)
{
int position=(sortedarray.length-1) / 7; // calculating the search key index i.e. (n/7)
int start=0;
int end=sortedarray.length-1;
while(start<=end)
{
int mid=(start+end)/2 //calculating the midpoint of the array
if(position==sortedarray[mid])
{
position=position*4; //setting the index to the next position after (n/7)
return mid;
}
elseif(position<sortedarray[mid])
{
end=mid-1; // searching to left hand side of the mid point of the array
}
else
{
start=mid+1; // searching to the right hand side of the mid point of the arrray
}
}
return -1;
