0%
0 / 7 answered
Searching Algorithms Practice Test
•7 QuestionsQuestion
1 / 7
Q1
public class SearchAnalyzer { private static int comparisons = 0;
public static boolean mysterySearch(int[] arr, int target) {
comparisons = 0;
return search(arr, target, 0, arr.length - 1);
}
private static boolean search(int[] arr, int target, int low, int high) {
if (low > high) return false;
int mid = (low + high) / 2;
comparisons++;
if (arr<u>mid</u> == target) return true;
boolean leftResult = search(arr, target, low, mid - 1);
boolean rightResult = search(arr, target, mid + 1, high);
return leftResult || rightResult;
}
public static int getComparisons() { return comparisons; }
}
What type of search algorithm does the mysterySearch method implement, and how many comparisons will be made when searching for a value that exists at the last position of a sorted array with 15 elements?
What type of search algorithm does the mysterySearch method implement, and how many comparisons will be made when searching for a value that exists at the last position of a sorted array with 15 elements?