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 min

Let’s break it down:

  • min := a1 β†’ assume first element is smallest

  • loop 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 ← xi

Used 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/2

Repeats while condition is true.


3⃣ repeat-until Loop

Runs first, checks later.

Repeat
    Output i
Until i = 0

Key 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.