Join the growing community of curious minds on IDNLearn.com. Ask any question and receive accurate, in-depth responses from our dedicated team of experts.
Sagot :
Answer:
class Main {
public static void main(String[] args) {
int[] values = {11,12,15,16,112,118,123,145};
int target = 15;
int min = 0;
int high = values.length-1;
boolean found = false;
int answer = 0;
int mid;
while(!found && min <= high) {
mid = (min + high) / 2;
if (values[mid] == target) {
found = true;
answer = mid;
} else if (target > values[mid]) {
min = mid + 1;
} else {
high = mid - 1;
}
}
if (found) {
System.out.printf("%d FOUND AT ARRAY INDEX %d", target, answer);
} else {
System.out.printf("%d was not found", target);
}
}
}
Explanation:
I altered the while expression to make the code work.
Your engagement is important to us. Keep sharing your knowledge and experiences. Let's create a learning environment that is both enjoyable and beneficial. For trustworthy answers, rely on IDNLearn.com. Thanks for visiting, and we look forward to assisting you again.