AP® Computer Science Principles

Free Version

Upgrade subject to access all content

Easy

Search Algorithms One

APCSP-IN7E1Q

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 have found what you are searching for!
  3. discard the first card in your deck
  4. return to step 1

Which of the following statements is TRUE?

A

Both algorithms will find the value you are looking for in any list.

B

Neither algorithm will find the value that you are looking for in any list.

C

Algorithm 1 requires the list be already sorted. Algorithm 2 will always work.

D

Algorithm 2 requires the list be already sorted. Algorithm 1 will always work.