ALGORITHMS
📌 ALGORITHMS – MINI STUDY GUIDE (IT2212)
1⃣ What is an Algorithm?
According to your handout (page 1):
An algorithm is a finite sequence of well-defined steps used to perform a task.
In simple terms:
An algorithm is just a step-by-step recipe to solve a problem.
Think of it like:
Cooking instructions 🍳
Google Maps directions 🗺
Workout routine 🏋
If the steps are clear and finite (may ending), that’s an algorithm.
🔎 Example from the Handout: Finding the Smallest Number
Set: {3, 5, 6, 1, 2, 7}
What did the algorithm do?
Compare 3 and 5 → keep 3
Compare 3 and 6 → keep 3
Compare 3 and 1 → keep 1
Compare 1 and 2 → keep 1
Compare 1 and 7 → keep 1
Final Answer: 1
✨ Notice something?
It keeps a temporary "current smallest" value and updates it.
That’s how most minimum-finding algorithms work.
🧠 Key Characteristics of an Algorithm
From page 2 (Rosen, 2019)
Property | Meaning (Easy Version) |
|---|---|
Input | It accepts data |
Output | It produces an answer |
Definiteness | Steps are clear (walang malabo) |
Correctness | Always gives the right answer |
Finiteness | It stops eventually |
Effectiveness | Steps are doable |
Generality | Works for all similar problems |
🎯 Example Connection
If I say:
“Keep subtracting 1 forever.”
Is that an algorithm?
❌ NO — it fails Finiteness (never stops).
🧠 Memory Trick:
I Owe DC FEG
I – Input
O – Output
D – Definiteness
C – Correctness
F – Finiteness
E – Effectiveness
G – Generality
2⃣ PSEUDOCODE
From page 1
Pseudocode is like fake code that looks like programming but isn’t tied to any language.
Why use it?
Because:
Python looks different from Java
Java looks different from C++
So instead of writing real code, we write something universal.
Example from the Handout
procedure min(a1, a2, a3, …, an : integers)
min := a1
for i := 2 to n
if min > ai then
min := ai
return minLet’s break it down:
min := a1→ assume first element is smallestloop from 2 to n
compare each element
update min if needed
✨ This part is important kasi ito ‘yung foundation ng searching algorithms.
3⃣ FLOWCHARTS
From page 1–2
A flowchart is a visual representation of an algorithm.
It’s read:
➡ Top to bottom
➡ Left to right
🔷 Common Flowchart Symbols
Symbol | Meaning |
|---|---|
Oval | Start / Stop |
Rectangle | Process |
Diamond | Decision |
Parallelogram | Input / Output |
Arrow | Flow direction |
Real-life analogy:
Flowchart = Waze navigation
Start → Decision (Traffic?) → Choose route → Arrive
4⃣ CONTROL STRUCTURES
This is SUPER IMPORTANT for exams.
Control structures tell the algorithm:
What to execute
When to execute
How many times to execute
From page 2–3
There are TWO major types:
A. Conditional Controls (Decision-Making)
Used when we need choices.
1⃣ if-then
If x ≥ 60 then
Output "Passed"Only runs if condition is true.
2⃣ if-then-else
If x ≥ 60 then
Output "Passed"
else
Output "Failed"This one gives two possible outcomes.
Real-life example:
If may pera ka → order food
Else → tubig muna 😭
B. Loop Controls (Repetition)
Used when we repeat steps.
1⃣ for-do Loop
Used when we know how many times to repeat.
Example from page 3 (finding minimum)
For i = 2 to n do
If xi < min then
min ← xiUsed for counting loops.
2⃣ while-do Loop
Used when repetition depends on condition.
Example (page 3): Dividing by 2 repeatedly
While n is even do
n ← n/2Repeats while condition is true.
3⃣ repeat-until Loop
Runs first, checks later.
Repeat
Output i
Until i = 0Key difference:
while → checks before running
repeat → runs first, checks after
🔥 Quick Comparison Table
Loop Type | When to Use | Condition Checked |
|---|---|---|
for | Known number of repetitions | Controlled by counter |
while | Condition-based repetition | Before loop |
repeat-until | Must run at least once | After loop |
Memory Trick:
FWR
F – Fixed (for loop)
W – Wait (while checks first)
R – Run first (repeat runs first)
5⃣ TRACING AN ALGORITHM
From page 3–4
Tracing = manually running the algorithm step-by-step.
Why do we trace?
To check correctness
To find errors
To understand variable changes
Example (Pass/Fail)
Input: 75
75 ≥ 60 → YES → Passed
Input: 50
50 ≥ 60 → NO → Failed
Example (Finding Minimum)
Set: {5, 4, 8, 3}
Step-by-step:
min = 5
compare with 4 → min = 4
compare with 8 → still 4
compare with 3 → min = 3
Final answer: 3
Tracing shows how variables change.
💡 COMMON EXAM CONFUSIONS
❓ Algorithm vs Pseudocode
Algorithm = concept
Pseudocode = how we write it
❓ While vs Repeat
While → might not run at all
Repeat → runs at least once
❓ Why must algorithms be finite?
Because infinite loops = useless program.
🧠 FINAL RECAP (Memory Hacks Mode)
If you remember NOTHING else, remember this:
1⃣ Algorithm = Finite + Clear + Stops
Think: Recipe with ending.
2⃣ 7 Properties → “I Owe DC FEG”
Input
Output
Definiteness
Correctness
Finiteness
Effectiveness
Generality
3⃣ Control Structures = CD + Loops
Conditional → if, if-else
Loops → for, while, repeat
4⃣ Loop Memory Trick → FWR
For = Fixed count
While = Wait/check first
Repeat = Run first
5⃣ Tracing = Manual simulation
If you can trace it, you understand it.
🧠 30-Second Master Summary
An algorithm is a finite, precise set of steps that solves a problem.
It must have input, output, correctness, and must stop.
We write it using pseudocode and visualize it using flowcharts.
Control structures (if, loops) control decision-making and repetition.
Tracing helps test if the algorithm works properly.