?

AP® Computer Science Principles

Free Version

Upgrade subject to access all content

Moderate

Search Algorithms Two

APCSP-4EG4XI

Consider the following algorithms. Assume that you have a deck of cards with numbers on them and you are looking for a card with a particular number:

Algorithm 1:

  1. look at the middle card in your deck.
  2. if correct, done! You have found what you are searching for!
  3. if you are looking for a larger number,discard the lower half of the deck
  4. else, discard the upper half of the deck
  5. return to step 1

Algorithm 2:

  1. look at the first card in your deck.
  2. if this is what you are looking for, great! You found what are searching for!
  3. discard the first value in your deck
  4. return to step 1

Which of the following statements is TRUE?

A

Algorithm 1 is an example of linear search. It will work for any list.
Algorithm 2 is an example of binary search. For it to work, the list must already be sorted.

B

Algorithm 1 is an example of linear search. For it to work, the list must already be sorted.
Algorithm 2 is an example of binary search. It will work for any list.

C

Algorithm 1 is an example of binary search. It will work for any list.
Algorithm 2 is an example of linear search. For it to work, the list must already be sorted.

D

Algorithm 1 is an example of binary search. For it to work, the list must already be sorted.
Algorithm 2 is an example of linear search. It will work for any list.