Chapter 1
Algorithms
Objectives
Understanding the concept of an algorithm.
Exploring various examples of algorithms.
Distinguishing between actions and control statements in algorithms.
Examining the differences between algorithms designed for human understanding versus those for computer execution.
Understanding the process by which an algorithm transforms into a computer program.
Clarifying the distinction between problems and algorithms.
What is an Algorithm?
Definition: An algorithm is defined as an ordered list of actions designed to solve a specific problem or perform a particular task.
Example 1: Making Ramen
Boil water.
Add noodles to the boiling water.
Wait 6-8 minutes.
Drain the noodles.
Stir in contents of flavor packet.
Place cooked noodles in a bowl.
Actions in Algorithms
An algorithm is characterized by a sequence of actions that must be adhered to for it to function correctly.
Requirements for Actions:
Imperative: Each action must be phrased as a clear command instructing what to do.
Feasible: The action must be executable by the intended recipient (e.g., a child, adult, or computer).
Self-explanatory: Each action must be easily understandable without requiring additional explanation to execute.
Recipient Considerations:
The effectiveness of an action in an algorithm is contingent upon the capabilities of the recipient (e.g., whether it’s a human or a computer).
Example: The action "Sort the list of numbers in ascending order"
Feasible for a computer or trained programmer.
Not feasible for a child who may not have the ability to compare numbers or understand the sorting process.
Control Flow of Algorithms
Example Algorithm: PlayJenga
Stack the blocks to form a tower.
Choose a current player.
While the tower of blocks is still standing:
Current player takes a block from the middle.
Current player puts the block on top of the tower.
If the tower is still standing:
Make the other player the current player.
The current player loses if the tower falls.
Methods of Writing Algorithms
Natural Language: Allows for a more conversational style but may lack precision.
Pseudocode: A structured format that combines natural language and programming concepts, providing a clear outline.
Programming Language: Utilized for implementation in computer programs; follows specific syntax and rules.
Images and Visual Instructions: Utilizes diagrams or visuals to convey the steps involved in an algorithm.
Algorithms for Humans vs Algorithms for Computers
Aspect | Humans | Computers |
|---|---|---|
Written In | Natural language, pseudocode, images | Programming languages |
Flexibility | Easy to understand and explain the logic | Strict and unambiguous |
Examples | Cooking recipes, manuals | Computer codes |
Problems vs. Algorithms
Problems:
Definition: Represents a task that requires execution.
Description: There may be multiple algorithms available to address a specific problem.
Algorithms:
Definition: A specific and structured set of steps designed to carry out a task.
Description: Each algorithm provides a solution to only one defined problem.