Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Practice Question 4: Which statement is correct about the following Binary Searc

ID: 3605715 • Letter: P

Question

Practice Question 4: Which statement is correct about the following Binary Search code (which we discussed in class) for finding a value in a sorted array?

public static int binarySearch(int anArray[], int first, int last, int value) {

int index;

if (first > last) {

index = -1; }

else {

int mid = (first + last)/2;

if (value == anArray[mid]) {

index = mid; }

else if (value < anArray[mid]) {

// point X

index = binarySearch(anArray, first, mid-1, value);

}

else {

// point Y

index = binarySearch(anArray, mid+1, last, value);

} // end if

} // end if

return index;

} // end binarySearch

a) The code is wrong because there is no base case for the recursion.

b) The code may sometimes be an infinite loop.

c) The code is illegal because there are two recursive calls to binarySearch in it.

d) If value appears in anArray somewhere in position that is first and also last, then binarySearch will return the position at which value appears. Otherwise, it will return -1.

e) If the value searched for is in anArray[first], then binarySearch will not work correctly if first and last are equal.

Practice Question 5: In C, when you pass an array as an argument to a function, what actually gets passed?

a) The address of the first element of the array

b) The value of the first element of the first element of the array

c) All the elements of the array

d) NULL

e) None of the Above

Explanation / Answer

The code implemented in the question is correct. So the answer is option is d)

Question:4:

d) If value appears in anArray somewhere in position that is first and also last, then binarySearch will return the position at which value appears. Otherwise, it will return -1.

Question:5:

c) All the elements of the array

**Comment for any further queries.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote