A girl and a robot thinking together

Think it through · Start with a story

To find the same number, the robot can turn over cards one by one, or check the middle each time if the cards are sorted.

Break Down the Process

Follow the numbered steps, then explain them in your own words
  1. Method ACheck each card
  2. Method BHalve the range if sorted
  3. Compare requirementsAre the cards in order?
  4. Compare the costHow many cards must be checked?

One problem may have several algorithms with different requirements and amounts of work.

First check whether each method is guaranteed to be correct, then compare their speed. Binary search relies on sorted order, so it cannot be used on just any pile of cards.

01Can One Problem Really Have Many Solutions?

Absolutely.

For example, to find a target value in an array:

  • You can use linear search on an unsorted array.
  • You can use linear search on a sorted array.
  • You can also use binary search on a sorted array.

Each of these can produce the correct answer.

02Why Compare Correct Algorithms?

Speed

How much work is required?

Memory

How much extra space is needed?

Requirements

Must the data be sorted, for example?

Implementation complexity

Is it easy to write correctly and maintain?

03How Do You Choose Between Linear and Binary Search?

Linear search

No sorting needed; O(n); simple and direct.

Binary search

Requires sorted data or a usable monotonic property; O(log n).

If you sort the data at great cost just to search once, binary search may not be faster overall.

04Why Does Input Size Matter?

Consider the same O(n²) algorithm:

n=100→about 10,000 operations;n=100,000→about 10,000,000,000 operations

Choose an algorithm based on the largest input size in the problem.

05Is the Fastest Algorithm Always Best?

Not necessarily.

For a very small data set, a simple O(n) method may be easier to get right and fast enough, compared with a more complicated structure.

06Why Learn Slow Algorithms First?

Simple algorithms help us:

  • Build a correct model of the problem.
  • Verify the result of an optimized algorithm.
  • Spot where repeated work happens.
  • Understand why a faster method is needed.

07How Do You Build the Habit of Choosing an Algorithm?

Make sure it is correct→Estimate input size and workload→Then decide whether to optimize

What You Have Learned

  • One problem can have several correct algorithms.
  • Algorithms can be compared by time, space, requirements, and implementation difficulty.
  • Input size changes which algorithm is suitable.
  • The fastest method is not always best in every real situation.
  • Simple algorithms are an important foundation for understanding and checking complex ones.

Topic complete: Next, explore “Search and Algorithm Efficiency.”

Your Turn to Try

If the cards are completely shuffled, can you discard the left half just because the middle card is smaller than 7?

Have you decided? Open to see the explanation

No. The left half could still contain a 7. Without sorted order, you cannot rule it out this way.