Algorithmic Design

Learn to create step-by-step procedures for solving problems systematically. Algorithmic thinking is the foundation of programming and systematic problem-solving in any domain.

Core Skill: Algorithmic design is about creating clear, unambiguous, step-by-step instructions that can be followed by anyone (including computers) to solve a problem consistently and efficiently.

What is Algorithmic Design?

Algorithmic design is the process of creating precise, step-by-step procedures (algorithms) to solve problems. An algorithm is like a recipe - it provides clear instructions that, when followed correctly, will consistently produce the desired result.

Key Characteristics of Good Algorithms

🎯 Precise

Each step is clearly defined with no ambiguity about what to do

⚡ Efficient

Solves the problem using reasonable time and resources

🔄 Reliable

Produces consistent results every time when given the same input

Algorithm vs. General Instructions

Making Coffee (Vague):

  1. Get some coffee
  2. Add water
  3. Make it hot
  4. Drink when ready

Problems:

  • How much coffee?
  • How much water?
  • How hot?
  • How long to heat?
  • When is it "ready"?
Making Coffee (Algorithmic):
  1. Measure 2 tablespoons of ground coffee
  2. Place coffee in filter
  3. Measure 1 cup (8 oz) of cold water
  4. Pour water into coffee maker reservoir
  5. Turn on coffee maker
  6. Wait until brewing cycle completes (about 5 minutes)
  7. Pour coffee into cup
  8. Add milk/sugar if desired

Benefits:

  • Specific measurements
  • Clear sequence
  • Defined completion criteria
  • Repeatable results
## Elements of Algorithm Design

1. Input and Output Definition

Before designing an algorithm, clearly define what goes in and what should come out:

Problem: Find the largest number in a list

Input: A list of numbers [5, 2, 9, 1, 7, 6]
Output: The largest number (9)

Problem: Calculate student grade average

Input: List of test scores [85, 92, 78, 94, 88]
Output: Average score (87.4)

Problem: Organize a to-do list by priority

Input: Tasks with priority levels
Output: Ordered list from highest to lowest priority

2. Step-by-Step Procedure

Break the solution into clear, sequential steps:

Example: Finding Maximum Number Algorithm

Problem: Find the largest number in a list

Algorithm:

  1. Start with the first number as the current maximum
  2. For each remaining number in the list:
  3.     a. Compare it to the current maximum
  4.     b. If it's larger, make it the new maximum
  5.     c. If not, keep the current maximum
  6. After checking all numbers, return the maximum

Trace Through Example:

  • List: [5, 2, 9, 1, 7, 6]
  • Step 1: max = 5
  • Step 2a: Compare 2 to 5 → 2 < 5, keep max = 5
  • Step 2b: Compare 9 to 5 → 9 > 5, update max = 9
  • Step 2c: Compare 1 to 9 → 1 < 9, keep max = 9
  • Step 2d: Compare 7 to 9 → 7 < 9, keep max = 9
  • Step 2e: Compare 6 to 9 → 6 < 9, keep max = 9
  • Step 3: Return 9

3. Control Structures

Algorithms use specific patterns to control the flow of execution:

Description: Steps performed one after another in order

Example - Making a Sandwich:

  1. Get two slices of bread
  2. Open jar of peanut butter
  3. Spread peanut butter on first slice
  4. Open jar of jelly
  5. Spread jelly on second slice
  6. Put slices together
  7. Cut sandwich in half

When to Use: When steps must happen in a specific order Description: Different actions based on conditions

Example - Choosing Transportation:

  1. Check the distance to destination
  2. IF distance < 1 mile THEN walk
  3. ELSE IF distance < 5 miles THEN bike
  4. ELSE take car

When to Use: When the solution depends on different circumstances Description: Repeat actions until a condition is met

Example - Studying for a Test:

  1. Read study material
  2. Take practice quiz
  3. WHILE score < 90%:
  4.     a. Review missed questions
  5.     b. Study related material
  6.     c. Take another practice quiz
  7. Stop studying when score ≥ 90%

When to Use: When you need to repeat actions until a goal is achieved

Algorithm Design Process

Step 1: Understand the Problem

Problem Analysis Checklist:
□ What exactly needs to be solved?
□ What information do I have (inputs)?
□ What result do I need (outputs)?
□ Are there any constraints or limitations?
□ What would success look like?
□ Are there edge cases to consider?

Step 2: Design the Algorithm

Design Strategies

Top-Down Approach:

  1. Start with the main goal
  2. Break it into major steps
  3. Break each major step into smaller steps
  4. Continue until each step is simple and clear

Bottom-Up Approach:

  1. Identify the basic operations needed
  2. Combine basic operations into larger functions
  3. Build up to the complete solution

Pattern-Based Approach:

  1. Recognize which problem pattern this fits
  2. Apply the known algorithm pattern
  3. Adapt the pattern to specific requirements

Step 3: Test the Algorithm

Always test your algorithm before implementing it:

Testing Process:
1. Trace through with simple examples
2. Test edge cases (empty inputs, very large inputs)
3. Verify the algorithm handles error conditions
4. Check efficiency with larger examples
5. Confirm it produces correct outputs for all valid inputs

Common Algorithm Patterns

1. Search Algorithms

Finding specific items in collections:

Linear Search Algorithm

Problem: Find if a specific item exists in a list

Algorithm:

  1. Start at the beginning of the list
  2. For each item in the list:
  3.     a. If the item matches what we're looking for, return "found"
  4.     b. If not, move to the next item
  5. If we reach the end without finding it, return "not found"

Example: Looking for “banana” in [“apple”, “banana”, “cherry”]

  • Check "apple" ≠ "banana", continue
  • Check "banana" = "banana", return "found"
Binary Search Algorithm

Problem: Find item in a sorted list (more efficient)

Algorithm:

  1. Start with the middle item of the sorted list
  2. If middle item matches target, return "found"
  3. If target is smaller, search the left half
  4. If target is larger, search the right half
  5. Repeat with the chosen half until found or no items left

Example: Looking for 7 in [1, 3, 5, 7, 9, 11, 13]

  • Middle = 7, matches target, return "found"

Example: Looking for 5 in [1, 3, 5, 7, 9, 11, 13]

  • Middle = 7, target 5 < 7, search left half [1, 3, 5]
  • Middle = 3, target 5 > 3, search right half [5]
  • Check 5 = 5, return "found"

2. Sorting Algorithms

Organizing items in order:

Selection Sort Algorithm

Problem: Arrange numbers in ascending order

Algorithm:

  1. For each position in the list (from first to second-to-last):
  2.     a. Find the smallest number in the remaining unsorted part
  3.     b. Swap it with the number at the current position
  4. Repeat until all positions are filled

Example: Sort [64, 25, 12, 22, 11]

  • Position 0: Find min(64,25,12,22,11) = 11, swap → [11, 25, 12, 22, 64]
  • Position 1: Find min(25,12,22,64) = 12, swap → [11, 12, 25, 22, 64]
  • Position 2: Find min(25,22,64) = 22, swap → [11, 12, 22, 25, 64]
  • Position 3: Find min(25,64) = 25, no swap → [11, 12, 22, 25, 64]
  • Done: [11, 12, 22, 25, 64]

3. Optimization Algorithms

Finding the best solution:

Greedy Algorithm Pattern

Approach: Make the best choice available at each step

Example Problem: Making change with fewest coins

Algorithm:

  1. Start with the target amount
  2. While amount > 0:
  3.     a. Choose the largest coin that doesn't exceed remaining amount
  4.     b. Add that coin to the result
  5.     c. Subtract coin value from remaining amount
  6. Return the list of coins

Example: Make change for 67 cents using [25¢, 10¢, 5¢, 1¢]

  • 67¢: Use 25¢ → remaining 42¢
  • 42¢: Use 25¢ → remaining 17¢
  • 17¢: Use 10¢ → remaining 7¢
  • 7¢: Use 5¢ → remaining 2¢
  • 2¢: Use 1¢ → remaining 1¢
  • 1¢: Use 1¢ → remaining 0¢
  • Result: 25¢ + 25¢ + 10¢ + 5¢ + 1¢ + 1¢ = 67¢ (6 coins)

Real-World Algorithm Design

Example: Planning a Road Trip

Let’s design an algorithm for planning an efficient road trip:

Problem: Plan a road trip visiting multiple cities with minimum driving time

Input: 
- Starting city
- List of cities to visit
- Distance/time between each pair of cities
- Available travel days

Output:
- Ordered list of cities to visit
- Total travel time
- Daily itinerary

Algorithm:
1. Identify all required stops (cities to visit)
2. Calculate distances between all city pairs
3. Apply optimization strategy:
   a. Start from home city
   b. For each unvisited city, calculate total additional distance
   c. Choose the city that adds minimum total distance
   d. Mark city as visited
   e. Repeat until all cities visited
   f. Return to home city
4. Group cities by travel days based on daily driving limit
5. Create daily itinerary with stops and estimated times

Example: Organizing a Digital Photo Collection

Problem: Organize thousands of photos automatically

Input: Folder containing unsorted photos with various formats

Output: Organized folder structure by date and event

Algorithm:
1. Scan all files in the source folder
2. For each file:
   a. Check if it's a valid image format
   b. Extract creation date from metadata
   c. Extract location data if available
   d. Analyze image content for scene type (optional)
3. Create folder structure: Year/Month/
4. Group photos by date and potential events:
   a. If multiple photos within same hour = potential event
   b. If location data similar = same event
5. Move photos to appropriate folders
6. Generate summary report of organization results

Algorithm Analysis

Measuring Algorithm Quality

Definition: Does the algorithm solve the problem correctly?

Testing Methods:

  • Test with known examples
  • Test edge cases (empty input, very large input)
  • Test boundary conditions
  • Verify mathematical properties

Example: For a sorting algorithm

  • Test with sorted list (should remain unchanged)
  • Test with reverse-sorted list
  • Test with duplicate elements
  • Test with single element
  • Test with empty list
Time Efficiency: How long does it take?

Space Efficiency: How much memory does it use?

Comparison Example - Finding Maximum:

  • Method 1: Compare each number to every other number
  • Time: Very slow for large lists
  • Space: Uses constant memory
  • Method 2: Single pass through list keeping track of maximum
  • Time: Fast, looks at each number once
  • Space: Uses constant memory

Method 2 is clearly better Definition: How easy is the algorithm to understand and implement?

Factors:

  • Number of steps
  • Complexity of each step
  • How intuitive the approach is
  • How well it can be explained

Example: Bubble sort vs. Quick sort

  • Bubble sort: Simple to understand, easy to implement
  • Quick sort: More complex, harder to implement correctly
  • Trade-off: Simplicity vs. efficiency
## Algorithm Design Exercises

Exercise 1: Design Your Own Algorithm

Choose one of these problems and design a complete algorithm:

  1. Problem: Plan the most efficient route for grocery shopping

    • Input: Shopping list, store layout, current location
    • Output: Ordered list of aisles/sections to visit
  2. Problem: Organize your digital music collection

    • Input: Folder of music files with inconsistent naming
    • Output: Organized folders by artist/album
  3. Problem: Schedule study time for multiple subjects

    • Input: List of subjects, time available, exam dates
    • Output: Daily study schedule
Algorithm Design Template

Problem Statement: _______________

Inputs:

  • _______________
  • _______________

Outputs:

  • _______________

Algorithm Steps:

  1. _______________
  2. _______________
  3. _______________
  4. _______________

Test Cases:

  • Normal case: _______________
  • Edge case: _______________
  • Error case: _______________

Exercise 2: Algorithm Optimization

Take this simple algorithm and improve it:

Problem: Find duplicate items in a list

Simple Algorithm:
1. For each item in the list:
2.   For each other item in the list:
3.     If the two items are the same:
4.       Mark as duplicate
5. Return list of duplicates

Issues with this algorithm:
- Very slow for large lists
- Counts each duplicate multiple times
- Inefficient comparison method

Your Task: Design a more efficient algorithm for finding duplicates.

Improved Algorithm Example

Better Algorithm:

  1. Create an empty "seen" collection
  2. Create an empty "duplicates" collection
  3. For each item in the list:
  4.     a. If item is already in "seen", add to "duplicates"
  5.     b. If item is not in "seen", add to "seen"
  6. Return "duplicates" collection

Improvements:

  • Only looks at each item once
  • Each duplicate counted only once
  • Much faster for large lists

From Algorithms to Code

The Translation Process

Once you have a well-designed algorithm, converting it to code becomes straightforward:

Algorithm Step                    →    Code Pattern
==============                         ============
"For each item in list"           →    for item in list:
"If condition then action"        →    if condition:
"While condition repeat"          →    while condition:
"Store value in variable"         →    variable = value
"Return result"                   →    return result

Example Translation

Algorithm: Find average of numbers
1. Initialize sum to 0
2. Initialize count to 0  
3. For each number in the list:
   a. Add number to sum
   b. Add 1 to count
4. Calculate average = sum ÷ count
5. Return average

Python Code:
def calculate_average(numbers):
    sum_total = 0           # Step 1
    count = 0               # Step 2
    for number in numbers:  # Step 3
        sum_total += number # Step 3a
        count += 1          # Step 3b
    average = sum_total / count  # Step 4
    return average          # Step 5

Next Steps

Algorithmic design is the bridge between understanding a problem and implementing a solution. As you develop this skill:

  1. Practice regularly with different types of problems
  2. Study existing algorithms to learn common patterns
  3. Always test your algorithms before coding
  4. Focus on clarity first, optimize later
  5. Document your thinking process for future reference

Combined with decomposition, abstraction, and pattern matching, algorithmic design gives you the complete toolkit for computational thinking and problem-solving.


← Previous: Pattern Matching Back to Overview