Be sure you have read sections 1.6 and 2.3 of the textbook.
Do the following problems. You do not need to turn in answers for the ones marked ``Not to turn in''. Most such problems will be those for which the textbook provides an answer in the back of the book, so you can check your work.
// returns index where x was found, or -1 if not found
def binarySearch(a : Array[Int], x : Int) : Int = {
var left = 0
var right = a.size-1
var result = -1;
while ((left <= right) && (result == -1)) {
val mid = (left+right)/2
if (x < a(mid))
right = mid-1
else if (x > a(mid))
left = mid+1
else
result = mid
}
result
}
A slightly more formal way to say what this function does
is to say that the loop establishes the following postcondition:
If x occurs at least once in a, then a(result) equals x; otherwise result is -1.
Answer the following questions, using the two examples from class (4/30) as models.