Introduction to Algorithms
CSSD 111: Introduction to Computer Science
Prof. Eric Amankwa (PhD
Lecture Objectives
• Define what an algorithm is and understand its key characteristics
• Distinguish between good and bad algorithms
• Learn to represent algorithms using flowcharts and pseudocode
• Understand the four pillars of computational thinking
• Develop simple algorithms for everyday problems
• Prepare for the mid-semester examination (Week 7)
What is an Algorithm?
• An algorithm is a finite set of unambiguous, step-by-step instructions
for solving a problem or accomplishing a task.
• The word "algorithm" comes from the name of the 9th-century Persian
mathematician Muhammad ibn Musa al-Khwarizmi, who wrote
extensively on mathematical procedures .
• Examples of everyday algorithms:
• A recipe for cooking jollof rice
• Directions from your home to GCTU
• Instructions for changing a car tire
• The steps to perform long division
• Key Point: Algorithms are everywhere. We follow them daily without
thinking. Computer science is about formalizing these procedures so
machines can execute them
Characteristics of a Good Algorithm
• A good algorithm must have these properties:
Property Description
Must terminate after a finite number of steps.
Finite
No infinite loops!
Each step must be clear and precise, with no
Unambiguous
room for interpretation
Each step must be doable (practically
Effective
executable)
Input Takes zero or more inputs (data to process)
Produces at least one output (a result or
Output
solution)
Produces the correct solution for all valid
Correct
inputs
Efficient Uses reasonable time and resources
Example of a Good Algorithms
• Good Algorithm: Computing
Average of Three Numbers 1. Start
2. Read numbers A, B,
• Why this a Good Algorithm: C
• Finite (6 steps) 3. Sum = A + B + C
4. Average = Sum / 3
• Unambiguous (each step is
clear) 5. Print Average
• Produces correct output 6. End
Example of a Bad Algorithm:
• Find the largest number 1. Start
2. Guess the largest
• Why this is a Bad number
Algorithm: 3. If you guessed correctly,
• Not guaranteed to terminate stop
(could guess forever)
4. Otherwise, guess again
• Unclear what "guess 5. Go to step 3
correctly" means
• No systematic method
Algorithm Representation Methods
• We can represent algorithms in different ways:
Method Description Best For
Written in plain Communicating with
Natural Language
English/Twi people
Structured English
Planning code;
Pseudocode with programming
precise but readable
conventions
Visual diagrams Visualizing flow;
Flowcharts
using symbols understanding logic
Same Algorithm: Find the maximum of two
numbers
• Natural Language: • Flowchart:
"Take two numbers. Compare Start
them. The larger one is the
maximum.“ Read A,B,C
• Pseudocode:
Is A>B? Max=B
IF num1 > num2
THEN
Is Max=C
max = num1 Max>C?
ELSE
Print Max
max = num2
END IF End
Flowchart Symbols
Symbol Name Purpose
Start and end points of the
○ Terminal
algorithm
An action or operation
□ Process
(e.g., calculate, assign)
A conditional test (yes/no,
Decision
true/false)
Reading input or
/ Input/Output
displaying output
→ Flow Line Direction of flow (arrows)
Flowchart Example: Start
Find the Maximum Read A,B,C
• Algorithm: Find the largest of
three numbers (A, B, C) Is A>B? Max=B
Max=A
Is Max>C? Max=C
Print Max
End
Introduction to Pseudocode
• Pseudocode is a simplified, language-independent way to describe
algorithms. It uses programming-like structures but avoids strict
syntax rules.
Key Pseudocode Conventions:
Operation Pseudocode Example
Input READ variable or INPUT variable
Output PRINT message or DISPLAY variable
variable = value or SET variable TO
Assignment
value
Decision IF condition THEN ... ELSE ... END IF
FOR counter FROM start TO end ...
Loop
END FOR
Loop WHILE condition ... END WHILE
Pseudocode Example:
READ age
IF age >= 18 THEN
PRINT "You are eligible to vote"
ELSE
PRINT "You are not eligible to vote"
END IF
Pseudocode Example: Sum of Numbers
1. START
• Problem: Calculate the sum of
numbers from 1 to N, where N 2. PRINT "Enter a positive integer N"
is provided by the user. 3. READ N
4. SET sum = 0
5. SET counter = 1
6. WHILE counter <= N DO
7. sum = sum + counter
8. counter = counter + 1
9. END WHILE
10. PRINT "The sum from 1 to", N, "is",
sum
11. END
Trace (for N = 5):
iteration counter sum before sum after
Start 1 0 -
1 1 0 1
2 2 1 3
3 3 3 6
4 4 6 10
5 5 10 15
End 6 - 15
Output: "The sum from 1 to 5 is 15"
Algorithm Design Techniques
• Three main algorithm design approaches:
• 1. Sequential (Sequence)
• Steps executed in order, one after another
• No branching or repetition
• Example: Computing average of three fixed numbers
• 2. Conditional (Selection)
• Decision points that choose between paths
• Uses IF-THEN-ELSE structures
• Example: Checking if a number is even or odd
• 3. Iterative (Repetition/Loops)
• Repeating a block of instructions
• Count-controlled loops (For)
• Condition-controlled loops (While)
• Example: Summing all numbers in a list
• Combining Structures:
• Most real-world algorithms combine all three techniques. Complex
problems are solved by decomposing them into sequences, decisions,
and repetitions
Introduction to Computational Thinking
• Computational Thinking is the thought process involved in
formulating problems and their solutions so that they can be
effectively carried out by a computer .
• Computational thinking is not about programming—it's
about thinking like a computer scientist. It's a problem-solving
approach that you can apply in any field.
• When you encounter a difficult problem, ask: "How can I break this
down? What patterns exist? What's essential? What steps will solve
it?"
The Four Pillars:
Pillar Description Example
Planning a road trip: break into
Breaking a complex problem
Decomposition route planning, booking hotels,
into smaller, manageable parts
packing, etc.
Finding that many problems
Identifying similarities and
Pattern Recognition involve sorting, searching, or
trends within problems
counting
Focusing on essential details A map shows roads and
Abstraction while ignoring irrelevant landmarks but not every tree or
information building
Creating step-by-step Writing the procedure to
Algorithm Design instructions to solve the calculate travel time based on
problem distance and speed
Decomposition in Action
• Problem: Design a system for a university course registration system.
• Decomposition:
University Registration System
│
┌───────────────┼───────────────┐
↓ ↓ ↓
Student Module Course Module Payment Module
│ │ │
┌───┼───┐ ┌───┼───┐ ┌───┼───┐
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
Add Drop View Create List Validate Process
Student Student Schedule Course Courses Payment Payment
Why Decompose?
• Each sub-problem is simpler to solve
• Different team members can work on different modules
• Solutions can be tested independently
• Changes affect only the relevant module
Pattern Recognition
• Identifying common structures across different problems.
Problem 1 Problem 2 Problem 3
Find the tallest student Find the largest number Find the most expensive product
Same pattern: Find the maximum
value in a collection
Abstraction
• Removing unnecessary details to focus on the essence
• Example: Navigation Map
• Concrete: Roads, traffic lights, buildings, trees, cars, pedestrians
• Abstract: Points and lines (nodes and edges) representing locations
and connections
• Benefits of Abstraction:
• Simplifies problem-solving
• Enables reuse of solutions across domains
• Hides complexity from users
Practical Algorithm Development - Exercise
1: Finding the Minimum
• Design an algorithm to find the smallest number in a list of five
numbers.
• Think about:
• What inputs do you need?
• How do you compare values?
• How do you track the smallest so far?
Exercise 2: Even or Odd
• Design an algorithm that determines whether a number is even or odd.
• Think about:
• What operation helps identify evenness?
• What are the possible outputs?
Exercise 3: Counting Occurrences
• Design an algorithm to count how many times the letter 'a' appears in a
given word.
• Think about:
• How do you examine each letter?
• What do you do when you find an 'a'?
Solution: Finding the Minimum -
Algorithm
1. START
2. PRINT "Enter five numbers:"
3. READ num1, num2, num3, num4, num5
4. SET min = num1
5. IF num2 < min THEN min = num2
6. IF num3 < min THEN min = num3
7. IF num4 < min THEN min = num4
8. IF num5 < min THEN min = num5
9. PRINT "The smallest number is", min
10. END
Alternative (using a loop)
1. START
2. PRINT "Enter how many numbers:"
3. READ N
4. SET min = very large number
5. FOR counter FROM 1 TO N DO
6. PRINT "Enter number", counter
7. READ num
8. IF num < min THEN min = num
9. END FOR
10. PRINT "The smallest number is",
min
11. END
Solution: Even or Odd
• Algorithm: Determine if a 1. START
Number is Even or Odd 2. PRINT "Enter a number:"
• Key Insight: A number is even if it 3. READ number
is divisible by 2 with no remainder. 4. SET remainder = number MOD 2
We use the modulo operation 5. IF remainder == 0 THEN
(remainder after division). 6. PRINT number, "is even"
7. ELSE
8. PRINT number, "is odd"
9. END IF
10. END
Solution: Count Letter 'a' in a Word
1. START
2. PRINT "Enter a word:"
3. READ word
4. SET count = 0
5. SET position = 1
6. WHILE position <= length of word DO
7. SET letter = the character at position 'position' in word
8. IF letter == 'a' THEN
9. count = count + 1
10. END IF
11. position = position + 1
12. END WHILE
13. PRINT "The letter 'a' appears", count, "times"
14. END
Trace for word = "banana":
Position Letter Letter == 'a'? Count
1 b No 0
2 a Yes 1
3 n No 1
4 a Yes 2
5 n No 2
6 a Yes 3
Output: "The letter 'a' appears 3 times"
Common Algorithm Patterns
• Pattern 1: Accumulation
• Keeping a running total
• Example: Sum, count, average
• Pattern 2: Maximum/Minimum
• Tracking the largest or smallest so far
• Example: Finding the highest score
• Pattern 3: Search
• Checking if an item exists in a collection
• Example: Looking up a name in a list
• Pattern 4: Sorting
• Arranging items in a specific order
• Example: Alphabetizing names
• Pattern 5: Filtering
• Selecting items that meet a condition
• Example: Finding all even numbers in a list
Exercise – Complex Algorithm Design
• Design an Algorithm for This • Design an algorithm that:
Problem • Reads the grades for the 5
• Problem Statement: courses
• A student has taken 5 courses • Calculates the Grade Point
with the following grading scale: Average (GPA)
• A = 4.0 • Displays the GPA
• B = 3.0 • Displays "Excellent" if GPA ≥
• C = 2.0 3.5, "Good" if GPA ≥ 2.5,
"Satisfactory" if GPA ≥ 1.5, and
• D = 1.0 "Needs Improvement" otherwise
• F = 0.0
Hints
• What inputs do you need?
• How do you convert letter grades to points?
• How do you compute the average?
• What conditional logic determines the message?
Solution: GPA 1. START
2. SET total_points = 0
Calculator 3. FOR course FROM 1 TO 5 DO
4. PRINT "Enter grade for course", course, "(A, B, C, D, F):"
Algorithm 5. READ grade
6. IF grade == 'A' THEN points = 4.0
7. ELSE IF grade == 'B' THEN points = 3.0
8. ELSE IF grade == 'C' THEN points = 2.0
9. ELSE IF grade == 'D' THEN points = 1.0
10. ELSE IF grade == 'F' THEN points = 0.0
11. ELSE PRINT "Invalid grade"
12. total_points = total_points + points
13. END FOR
14. SET gpa = total_points / 5
15. PRINT "Your GPA is:", gpa
16. IF gpa >= 3.5 THEN PRINT "Excellent"
17. ELSE IF gpa >= 2.5 THEN PRINT "Good"
18. ELSE IF gpa >= 1.5 THEN PRINT "Satisfactory"
19. ELSE PRINT "Needs Improvement"
20. END
Mid-Semester Exam Preparation
• Exam Format:
• Duration: 1 hour
• Weight: 20% of final grade
• Format: Multiple choice, short answer, and algorithm design
questions
Summary
• An algorithm is a finite, unambiguous set of steps for solving a
problem
• Properties of good algorithms: Finite, unambiguous, effective, with
defined inputs and outputs
• Algorithm representation: Use flowcharts (visual) or pseudocode
(structured English)
• Computational thinking has four pillars: Decomposition, Pattern
Recognition, Abstraction, Algorithm Design
• Three algorithm structures: Sequence, Selection, Iteration
• Common patterns: Accumulation, max/min, search, sorting, filtering
• Efficiency matters—different algorithms can solve the same problem
at vastly different speeds
Assignment
• Design an Algorithm and Flowchart for each of the problems below:
• Displaying the first names of N students in a class.
• Compute the average age of N students in a class
• Compute the GPA of a student who studies N courses in a semester.