A girl and a robot thinking together

Think it through · Start with a story

The girl wants to find the number 7 in a row of cards. The robot suggests checking them one by one from the left. That is a clear method.

Break Down the Process

Follow the numbered steps, then explain them in your own words
  1. PrepareSet the target to 7
  2. CheckIs this card a 7?
  3. ContinueIf not, check the next card
  4. FinishFind it or check them all

An algorithm is a clear set of steps for solving a problem.

The steps must be executable and have a condition for finishing. With this finite row of cards, checking them all lets us report whether the target was found.

01What Exactly Is an Algorithm?

An algorithm is a clear set of steps for solving a particular kind of problem.

For example, to find the largest number in a group, treat the first number as the current largest. Then compare each number from the second one onward, updating the largest whenever you find a bigger value.

02How Is an Algorithm Different from a Program?

Algorithm

Focuses on how to solve the problem.

Program

Implements the algorithm, along with input, output, and other details, in a programming language.

The same algorithm can be implemented in C++, Python, or other languages.

03What Should an Algorithm Do?

Clear steps

Each step says exactly what to do.

It finishes

For inputs in the specified range, it stops after a finite number of steps.

Correct result

It meets the requirements of the problem.

Clear scope

We know which inputs it handles.

04Is Being Correct Enough?

Not always. Two algorithms can both give the right answer, but one may need 100 operations while another needs a million.

05Does an Algorithm Have to Be Code?

No. You can first describe an algorithm in plain language, a flowchart, or pseudocode, then implement it as a program.

Treat the first number as the largest so far
Starting with the second number:
    If the current number is larger, update the largest
Output the largest number

06Are More Complex Algorithms Better?

No. A simple, clear method that solves the problem correctly is usually better.

What You Have Learned

  • An algorithm is a clear set of steps for solving a type of problem.
  • An algorithm and a program are not the same thing.
  • An algorithm should be clear, finish, and produce a correct result.
  • Algorithms also need to be considered for efficiency and scope.
  • You can describe an algorithm without writing code first.

Next: Enumeration: Try Every Possibility

Your Turn to Try

If none of the cards is a 7, should the algorithm keep searching or report that the target is absent?

Have you decided? Open to see the explanation

It should report that the target is absent. The stopping condition includes both finding the target and finishing the check of every possible position.