Welcome to the World of Algorithms!
Hello! This chapter is the heart of Computer Science. You might think algorithms are complex code, but they are just brilliant plans for solving problems. If you can follow a recipe, you can understand an algorithm!
In this section, we will learn how to properly plan and structure the steps a computer takes to solve any problem. This skill is essential for problem solving in programming and beyond.
Don't worry if this seems tricky at first. We will break down every concept into simple, manageable steps.
1. Defining Algorithms: The Computer's Recipe
What Exactly is an Algorithm?
An algorithm is simply a set of well-defined, step-by-step instructions used to solve a specific problem or complete a task.
If the steps are followed correctly, the algorithm must always produce the correct result and must eventually stop.
Key Characteristics of a Good Algorithm:
- Clear: Every step must be unambiguous (not confusing).
- Finite: It must finish after a limited number of steps. It can't run forever!
- Effective: Each instruction must be simple enough to be executed.
The Input-Process-Output (IPO) Model
Every program and every algorithm follows a basic structure known as the IPO Model:
INPUT → PROCESS → OUTPUT
Analogy: Making a Cup of Tea
- INPUT: Water, tea bag, milk, sugar. (The raw data/ingredients you start with).
- PROCESS: Boil water, steep tea bag, add milk/sugar. (The algorithm/steps).
- OUTPUT: A perfectly brewed cup of tea. (The result/solution).
In Computer Science, the input might be a number typed by the user, the process might be adding that number to another, and the output is the displayed result.
Quick Review: Algorithm Definition
An algorithm is a finite sequence of unambiguous steps to solve a problem. It follows the IPO model.
2. The Building Blocks: Control Structures
To control the flow of instructions, all algorithms use three fundamental structures. Think of these as the three main tools you have to write any program.
A. Sequence (Doing things in order)
Sequence is the most basic structure. Instructions are executed one after the other, from top to bottom.
- Step 1 is done, then Step 2, then Step 3, and so on.
- Example: Getting ready for school.
1. Wake up.
2. Brush teeth.
3. Put on uniform.
B. Selection (Making Choices)
Selection (or Decision) allows the algorithm to choose between two or more paths based on a condition being true or false. This usually uses IF... THEN... ELSE logic.
Analogy: A Traffic Light Decision
If the light is green (condition is true): THEN drive.
ELSE (if the light is not green, i.e., red or amber): THEN stop.
Key Point: Selection means only one path is followed.
Common Selection Structures:
- IF... THEN... (Action only taken if true)
- IF... THEN... ELSE (One action if true, a different action if false)
- Nested Selection (Putting one IF statement inside another, used for complex decisions)
C. Iteration (Repeating Actions)
Iteration (also called Repetition or Looping) means repeating a set of instructions until a certain condition is met.
We use iteration when we need to do the same task multiple times, like checking every name in a long list.
There are two main types of loops you need to know:
1. Fixed Iteration (FOR Loop)
Used when you know exactly how many times the loop needs to run.
Example: FOR 10 times, lift the weights.
2. Condition-Controlled Iteration (WHILE Loop)
Used when the number of repeats is unknown. The loop continues WHILE a condition remains true.
Example: WHILE the password is incorrect, keep asking the user to try again.
Warning for Students: Be careful with WHILE loops! If the condition never becomes false, the algorithm will run forever. This is called an infinite loop—a common programming mistake!
Memory Aid: The 3 S's & I
Algorithms rely on:
1. Sequence (Order)
2. Selection (Choice)
3. Iteration (Repeat)
3. Representing Algorithms
Before you write code, you need to plan the algorithm. We use two main tools for visualising or writing the plan:
A. Flowcharts (Visual Planning)
A flowchart uses symbols and lines to show the flow of logic step-by-step. They are great for seeing the big picture, especially decisions (selection) and loops (iteration).
Key Flowchart Symbols You Must Know:
| Symbol | Name/Shape | What it Represents |
|---|---|---|
| Oval or Rounded Rectangle | Terminator | The START or END point of the algorithm. |
| Rectangle | Process | A step where data is manipulated or a calculation is performed (e.g., adding two numbers). |
| Parallelogram | Input/Output (I/O) | A step where the algorithm takes input from the user or displays output. |
| Diamond | Decision | A point where a condition is tested (e.g., IF X > 10). It has one entry point and two exit points (YES/NO or TRUE/FALSE). |
| Arrow | Flow Line | Shows the direction of flow through the algorithm. |
B. Pseudocode (Structured English)
Pseudocode is a method of writing out the steps of an algorithm using structured English. It is not a programming language, but it uses common keywords (like IF, WHILE, FOR, READ, PRINT) to make the structure clear.
It is excellent for easily transferring your plan directly into any programming language later.
Example Pseudocode Snippet:
READ Score
IF Score >= 50 THEN
PRINT "Pass"
ELSE
PRINT "Fail"
ENDIF
Did You Know?
The term "algorithm" is named after the Persian mathematician Muhammad ibn Musa al-Khwarizmi, who lived around 820 AD. He helped introduce algebra and systematic calculation to the world!
4. Common Problem-Solving Algorithms
In problem-solving, computers often need to manage large amounts of data. Two of the most common tasks are finding data and putting data in order.
A. Searching Algorithms
A searching algorithm is a method used to find a specific item within a collection of data.
1. Linear Search (or Serial Search)
This is the simplest search method. The algorithm checks every item in the list, one by one, from the beginning to the end, until the required item is found or the end of the list is reached.
- When to use it: On small lists, or lists that are not sorted.
- Limitation: It can be very slow if the list is huge, as it might have to check every single item.
2. Binary Search
This is a much faster and more efficient search method, but it has one critical requirement:
The data list MUST be sorted before a binary search can be performed.
Analogy: Guessing a Page in a Book
If you search for a word in a dictionary (which is sorted), you don't start at page 1. You open roughly to the middle.
If the word you are looking for comes *before* the middle page, you ignore the second half of the book. If it comes *after*, you ignore the first half.
Binary search works by:
- Finding the middle item in the list.
- Comparing the target item to the middle item.
- If they don't match, the algorithm immediately eliminates half of the remaining data.
- It repeats this process until the item is found.
Binary search is significantly faster than Linear Search for large, sorted datasets.
B. Sorting Algorithms
A sorting algorithm is a method used to arrange data elements in a specific order (e.g., numerical or alphabetical, ascending or descending).
Why do we need sorting?
We sort data to make it much easier and faster to search (as shown by Binary Search) and to present information clearly (e.g., student results ordered by rank).
Example Concept: Bubble Sort (Simplified)
Bubble sort is one of the simplest sorting methods. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It is called "Bubble" sort because small, light elements slowly 'bubble up' to the top of the list.
While very simple to understand, it is not very efficient for very large lists compared to other sorting algorithms.
Key Takeaway: Efficiency
When selecting an algorithm, efficiency matters!
Binary Search is efficient (fast) but requires sorted data.
Linear Search is inefficient (slow for large lists) but works on unsorted data.
You have successfully mastered the foundation of planning computational problems! Now you know how to instruct a computer using sequence, selection, and repetition. Well done!