Question: 9 . Refer to method search. / * * @param v an initialized array of integers * @param key the value to be found *

9. Refer to method search.
/** @param v an initialized array of integers
* @param key the value to be found
* Postcondition:
*- Returned value k is such that -1<= k <= v.length-1.
*- If k >=0 then v[k]== key.
*- If k ==-1, then key != any of the elements in v.
*/
public static int search(int[] v, int key)
{
int index =0;
while (index < v.length && v[index]< key)
index++;
if (v[index]== key)
return index;
else
return -1;
}
Assuming that the method works as intended, which of the following should be
added to the precondition of search?
(A) v is sorted smallest to largest.
(B) v is sorted largest to smallest.
(C) v is unsorted.
(D) There is at least one occurrence of key in v.
(E) key occurs no more than once in v.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!