Binary Search Algorithm-Multiple Choice Question
Question
Consider the following Binary Search Algorithm
upper = length - 1;
lower = 1;
while (upper >= lower)
{ mid_pt = (upper + lower) / 2;
if (data[mid_pt] < target) ; comparison A
{ lower = mid_pt + 1; }
else if (data[mid_pt] == target) ; comparison B
{ return mid_pt; }
else { upper = mid_pt – 1;}
}
//target not found
Give the values for lower, upper and mid-pt for each iteration of the while loop and state how many times will the two comparisons A and B be executed using this algorithm to find the value 77 in the following array? You may not need to complete all the rows.
|
[0] |
[1] |
[2] |
[3] |
[4] |
[5] |
[6] |
[7] |
[8] |
[9] |
[10] |
[11] |
[12] |
|
14 |
27 |
29 |
38 |
42 |
55 |
57 |
61 |
64 |
69 |
77 |
79 |
84 |
|
|
|
|
||||||||||
|
Lower |
Upper |
Mid-pt |
|
- |
- |
- |
|
- |
- |
- |
|
- |
- |
- |
|
- |
- |
- |
|
- |
- |
- |
Comparison A executed ------ times & Comparison B executed ------- times.
