Question
Need some java help with a question for my program. I worked with a partner so I'm not 100% sure on my answer I'm mainly
Need some java help with a question for my program. I worked with a partner so I'm not 100% sure on my answer I'm mainly here to double check.
1. Describe how your firstIndexOf() method in BinarySearchDeluxe.jav finds the first index of a key that equals the search key.
Here is the part of my program that relates to the question.
public static
if (a == null || key == null || comparator == null){
throw new NullPointerException("Cannot find first key index");
}
//if not found
if (a.length == 0){
return -1;
}
//Binary Search Algorithm
int left = 0;
int right = a.length - 1;
while (left + 1 < right){
int mid = left + (right - left) / 2;
if (comparator.compare(key, a[mid]) <= 0){
right = mid;
}
else{
left = mid;
}
}
if (comparator.compare(key, a[left]) == 0){
return left;
}
if (comparator.compare(key, a[right]) == 0) {
return right;
}
return -1;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started