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.