The reference of this question can be found in either textbook 3.1 Algorithm 3 and 3.3 Example 3 or online materials about Binary Search algorithm. Answer questions regarding the the binary search algorithm shown below.
procedure binary_search (x: integer, a1,a2,an: increasing integers)
i := 1 { i is the left endpoint of the search interval }
j := n { j is the right endpoint of the search interval}
while (i < j)
m := (i + j) / 2
if x > am then i := m + 1
else j := m
if x = ai, then location := i
else location :=0
return location {location is the index of the ai, if x is found in the list. If x is not found, the procedure output 0}
(a). Let x be 6, and the list of {an} be 1, 2, 3, 5, 6, 8. What is the value of m in the first iteration and second iteration of the while loop respectively? What is the output of the program?
(b). How many comparisons x > am are performed during the execution of the program in (a)?
(c). Determine if the following statement is correct and briefly explain. "The binary_search algorithm in this problem is a O(log n) in the worst case, where n is the number of elements in the list."