Binary Search

Help Questions

AP Computer Science Principles › Binary Search

Questions 1 - 10
1

A student uses iterative binary search on sorted array 5, 8, 12, 17, 21, 26, 30, 34, 41, 49, 55, 63, 70, 81 to find target 34; midpoint is $\lfloor(\text{low}+\text{high})/2\rfloor$. Steps: low=0, high=13, mid=6 (30)<34 so low=7; next mid=$\lfloor(7+13)/2\rfloor$=10 (55)>34 so high=9; next mid=$\lfloor(7+9)/2\rfloor$=8 (41)>34 so high=7; next mid=$\lfloor(7+7)/2\rfloor$=7 (34) found. Refer to the array provided above. How many iterations are required to locate the target element using binary search?

7 iterations are required.

3 iterations are required.

14 iterations are required.

4 iterations are required.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, the array [5, 8, 12, 17, 21, 26, 30, 34, 41, 49, 55, 63, 70, 81] is searched for 34, requiring students to count iterations carefully. Choice B is correct because exactly 4 iterations are needed: mid=6 (30), mid=10 (55), mid=8 (41), and finally mid=7 (34). Choice A is incorrect because it undercounts by one iteration - students might miss counting the final successful comparison. To help students: Create iteration tracking tables and emphasize that finding the target still counts as an iteration. Watch for: off-by-one errors in counting iterations and confusion about whether the successful find counts as an iteration.

2

An iterative binary search is performed on sorted array 1, 6, 9, 14, 20, 27, 35, 44, 50, 61, 73, 88, 94 to find target 44; midpoint is $\lfloor(\text{low}+\text{high})/2\rfloor$. Steps: low=0, high=12, mid=6 (35)<44 so low=7; next mid=$\lfloor(7+12)/2\rfloor$=9 (61)>44 so high=8; next mid=$\lfloor(7+8)/2\rfloor$=7 (44) found. Refer to the array provided above. During the binary search, what is the middle element for the first iteration?

27

35

44

50

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, the array [1, 6, 9, 14, 20, 27, 35, 44, 50, 61, 73, 88, 94] has 13 elements, and students must identify the first iteration's middle element. Choice B is correct because with low=0 and high=12, the midpoint calculation ⌊(0+12)/2⌋=6 gives index 6, which contains the value 35. Choice C is incorrect because 44 is the target value at index 7, not the middle element of the first iteration - students may confuse the target with the midpoint. To help students: Always start by calculating the initial midpoint before any comparisons, and use array visualization tools to highlight the middle element. Watch for: confusion between the target value and the middle element value, and errors in initial midpoint calculation.

3

A class uses iterative binary search on sorted array 4, 9, 13, 18, 21, 27, 33, 39, 45, 52, 60 to locate target 45; midpoint is $\lfloor(\text{low}+\text{high})/2\rfloor$. Steps: low=0, high=10, mid=5 (27)<45 so low=6; next mid=$\lfloor(6+10)/2\rfloor$=8 (45) found. Refer to the array provided above. During the binary search, what is the middle element for the second iteration?

33

39

45

52

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, the array [4, 9, 13, 18, 21, 27, 33, 39, 45, 52, 60] is searched for 45, and students must identify the middle element in the second iteration. Choice C is correct because in the second iteration with low=6 and high=10, the midpoint calculation ⌊(6+10)/2⌋=8 gives index 8, which contains the value 45. Choice B is incorrect because 39 is at index 7, not the calculated midpoint of the second iteration - students may confuse adjacent indices. To help students: Create tables showing low, high, mid values for each iteration, and practice calculating midpoints using integer division. Watch for: off-by-one errors in midpoint calculations and confusion about which iteration is being referenced.

4

A nearly complete sorted sequence is stored as array 101, 102, 103, 104, 105, 106, 108, 109, 110, 111; exactly one integer is missing. Students use iterative binary search on the expected range 101–111, using midpoint $\lfloor(\text{low}+\text{high})/2\rfloor$ and comparing arraymid to expected value (101+mid). Steps: low=0, high=9, mid=4: array4=105 matches expected 105 so low=5; mid=$\lfloor(5+9)/2\rfloor$=7: array7=109 but expected 108 so high=6; mid=$\lfloor(5+6)/2\rfloor$=5: array5=106 matches expected 106 so low=6; mid=$\lfloor(6+6)/2\rfloor$=6: array6=108 but expected 107, so missing value is 107. Refer to the array provided above. What is the position of the target element in the array after applying binary search?

Missing value 107 after 10 sequential checks.

Missing value 106 is identified.

Missing value 107 is identified.

No value is missing because the array is sorted.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, binary search is cleverly adapted to find a missing value by comparing actual array values with expected sequential values. Choice A is correct because the algorithm identifies that 107 is missing when array[6]=108 but the expected value at that position should be 107 in a complete sequence. Choice B is incorrect because 106 is present in the array at index 5 - students might misread the array or misunderstand the algorithm. To help students: Explain how binary search can be modified for different problems beyond simple element finding, and practice identifying patterns in sequences. Watch for: confusion about how the expected value is calculated and difficulty understanding this non-standard application of binary search.

5

A phone directory is alphabetically sorted: "Allen", "Baker", "Chen", "Diaz", "Evans", "Foster", "Gupta", "Hernandez", "Ibrahim", "Jones", "Khan", "Lopez". To find "Jones", iterative binary search uses midpoint $\lfloor(\text{low}+\text{high})/2\rfloor$: low=0, high=11, mid=5 ("Foster") < "Jones" so low=6; mid=$\lfloor(6+11)/2\rfloor$=8 ("Ibrahim") < "Jones" so low=9; mid=$\lfloor(9+11)/2\rfloor$=10 ("Khan") > "Jones" so high=9; mid=$\lfloor(9+9)/2\rfloor$=9 ("Jones") found. Refer to the array provided above. Why is binary search more efficient than linear search in this scenario?

It works best when the array is randomly ordered.

It checks elements sequentially from the first entry.

It halves the remaining search range each iteration.

It requires more comparisons as the array grows.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, a phone directory demonstrates binary search on alphabetically sorted names, highlighting the algorithm's efficiency advantage. Choice A is correct because binary search's key efficiency comes from eliminating half the remaining elements with each comparison, reducing time complexity to O(log n). Choice B is incorrect because it describes linear search behavior - binary search specifically avoids sequential checking by jumping to calculated midpoints. To help students: Compare the number of steps needed for binary vs linear search on the same array, showing how the difference grows with array size. Watch for: confusion between binary and linear search behaviors, and ensure students understand the logarithmic time complexity advantage.

6

A teacher demonstrates iterative binary search on sorted array "ant", "bee", "cat", "dog", "eel", "fox", "goat", "hawk", "ibis", "jaguar" to find target "eel"; midpoint is $\lfloor(\text{low}+\text{high})/2\rfloor$. Steps: low=0, high=9, mid=4 ("eel") found immediately. Refer to the array provided above. During the binary search, what is the middle element for the given iteration?

"eel"

"dog"

"cat"

"fox"

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, the sorted array of animal names demonstrates an optimal case where the target "eel" is found immediately at the first midpoint. Choice B is correct because with low=0 and high=9, the midpoint calculation ⌊(0+9)/2⌋=4 gives index 4, which contains "eel" - the target is found in just one iteration. Choice D is incorrect because "cat" is at index 2, not the calculated midpoint - students might count positions incorrectly. To help students: Emphasize that binary search can find elements in one iteration if they happen to be at the midpoint, demonstrating best-case scenarios. Watch for: miscounting array positions or assuming binary search always requires multiple iterations.

7

A student runs iterative binary search on the sorted array (ascending): index 0..11, A=2, 5, 9, 13, 18, 21, 27, 33, 38, 44, 49, 56. Target starts as 18 and ends found. Pseudocode uses mid=(low+high)/2. Steps: low=0, high=11 => mid=5 (A5=21)>18 => high=4; low=0, high=4 => mid=2 (A2=9)<18 => low=3; low=3, high=4 => mid=3 (A3=13)<18 => low=4; low=4, high=4 => mid=4 (A4=18)=target. Refer to the array provided above. How many iterations are required to locate the target element using binary search?

5 iterations are required.

3 iterations are required.

4 iterations are required.

12 iterations are required.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, we have a 12-element sorted array (indices 0-11) and are searching for the value 18, with the algorithm clearly showing four distinct iterations. Choice B is correct because it accurately counts all four iterations: (1) mid=5, A[5]=21>18, adjust high=4; (2) mid=2, A[2]=9<18, adjust low=3; (3) mid=3, A[3]=13<18, adjust low=4; (4) mid=4, A[4]=18=target, search completes. Choice A is incorrect because it undercounts the iterations, possibly missing the final iteration where the target is found. To help students: Create a table showing iteration number, low/high values, mid calculation, and comparison result. Emphasize that finding the target still counts as an iteration, and practice with arrays of different sizes to reinforce the logarithmic nature of binary search.

8

A teacher demonstrates iterative binary search on the sorted string array: index 0..9, S="Ava","Ben","Cleo","Dina","Eli","Fay","Gus","Hana","Ira","Jules". Target starts as "Hana" and ends found. Pseudocode: mid=(low+high)/2; if Smid < target then low=mid+1 else high=mid-1. Steps: mid=4 ("Eli"<"Hana") => low=5; mid=7 ("Hana"=target) => stop. Refer to the array provided above. What is the position of the target element in the array after applying binary search?

Position 7.

Position 8.

Position 6.

Position 4.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, we have a sorted string array with 10 elements (indices 0-9) and are searching for "Hana", which the algorithm finds at index 7. Choice B is correct because it identifies position 7 where "Hana" is located: first iteration calculates mid=4 where "Eli"<"Hana" (alphabetically), so low becomes 5; second iteration calculates mid=7 where S[7]="Hana" matches our target. Choice A is incorrect because it confuses the position with a nearby index, possibly due to an off-by-one error in understanding array indexing. To help students: Emphasize that array positions start at 0, and practice with string comparisons using alphabetical ordering. Use visual representations showing how the search space narrows with each iteration.

9

A phone book app uses iterative binary search on a sorted name array (ascending): index 0..15, S="Allen","Brown","Carter","Davis","Edwards","Fisher","Grant","Hall","Irwin","Johnson","Klein","Lopez","Miller","Nguyen","Owens","Patel". Target starts as "Lopez" and ends found. Pseudocode uses mid=(low+high)/2. Steps: mid=7 ("Hall"<"Lopez") => low=8; mid=11 ("Lopez"=target) => stop. Refer to the array provided above. Why is binary search more efficient than linear search in this scenario?

It requires scanning from both ends simultaneously.

It works best when the array is randomly ordered.

It checks every name sequentially until it matches.

It discards half the remaining names each iteration.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, the phone book app uses binary search on a sorted name array, demonstrating the key efficiency advantage of the algorithm. Choice A is correct because it identifies the fundamental property of binary search: with each comparison, it eliminates half of the remaining search space, leading to logarithmic time complexity O(log n). Choice B is incorrect because it describes linear search, which checks every element sequentially and has O(n) time complexity. To help students: Use visual demonstrations showing how the search space shrinks exponentially with binary search versus linearly with sequential search. Calculate and compare the maximum number of iterations needed for different array sizes to reinforce the efficiency difference.

10

A student uses iterative binary search on the sorted array (ascending): index 0..10, A=10, 14, 18, 21, 26, 30, 33, 39, 44, 48, 55. Target starts as 26 and ends found. Pseudocode: mid=(low+high)/2. Step 1: low=0, high=10 => mid=5 (A5=30)>26 => high=4. Refer to the array provided above. During the binary search, what is the middle element for the next iteration?

A[2] = 18.

A[1] = 14.

A[3] = 21.

A[4] = 26.

Explanation

This question tests understanding of binary search algorithm application in AP Computer Science Principles. Binary search divides a sorted array in half repeatedly to efficiently locate the target element, using the midpoint to guide each iteration. In this problem, we have an 11-element array (indices 0-10) searching for 26, and after the first iteration where mid=5 and A[5]=30>26, the search space is reduced to low=0 and high=4. Choice A is correct because the next middle element calculation is mid=(0+4)/2=2, and at index 2 we find A[2]=18. Choice C is incorrect because it suggests A[4]=26, but this would be the upper bound of our search space, not the middle element. To help students: Practice calculating midpoints with various low/high combinations and emphasize integer division. Create exercises where students must identify not just the mid index but also verify the value at that index.

Page 1 of 2