Table of Contents
Algorithms: Step-by-Step Problem Solving
An algorithm is a step-by-step procedure for solving a problem. Every program you write contains algorithms - they're the logical thinking behind the code!
What is an Algorithm?
An algorithm is like a recipe or instruction manual. It breaks down a complex problem into simple, clear steps that can be followed to reach a solution.
Algorithm Example: Making a Sandwich
Algorithm: Make Peanut Butter and Jelly Sandwich
1. Get bread, peanut butter, jelly, and knife
2. Open bread bag and take out 2 slices
3. Open peanut butter jar
4. Use knife to spread peanut butter on one slice
5. Clean knife
6. Open jelly jar
7. Use knife to spread jelly on other slice
8. Put slices together with spreads facing inward
9. Clean up and enjoy!
Problem-Solving Process
1. Understand the Problem
- What exactly needs to be solved?
- What are the inputs and expected outputs?
- Are there any constraints or special cases?
2. Break It Down
- Divide the big problem into smaller sub-problems
- Solve each small problem separately
- Combine solutions to solve the big problem
3. Plan Your Approach
- Write out the steps in plain English first
- Consider different approaches
- Choose the simplest solution that works
4. Implement and Test
- Write the code
- Test with different inputs
- Fix any issues
Common Algorithm Patterns
1. Linear Search - Finding Something
Look through items one by one until you find what you're looking for.
Linear Search Algorithm
function findStudent(students, targetName):
for i = 0 to length(students) - 1:
if students[i].name == targetName:
return i // Found at position i
return -1 // Not found
// Usage
students = [
{name: "Alice", grade: 85},
{name: "Bob", grade: 92},
{name: "Charlie", grade: 78}
]
position = findStudent(students, "Bob")
if position != -1:
print("Found Bob at position " + position)
else:
print("Bob not found")
2. Sorting - Putting Things in Order
Arrange items in a specific order (alphabetical, numerical, etc.).
Simple Bubble Sort Algorithm
function bubbleSort(numbers):
n = length(numbers)
// Go through the array multiple times
for i = 0 to n - 1:
// Compare adjacent elements
for j = 0 to n - i - 2:
// If they're in wrong order, swap them
if numbers[j] > numbers[j + 1]:
// Swap
temp = numbers[j]
numbers[j] = numbers[j + 1]
numbers[j + 1] = temp
return numbers
// Usage
unsorted = [64, 34, 25, 12, 22, 11, 90]
sorted = bubbleSort(unsorted)
print(sorted) // [11, 12, 22, 25, 34, 64, 90]
3. Counting - How Many of Something
Count occurrences of specific items or conditions.
Counting Algorithm
function countVowels(text):
vowels = ["a", "e", "i", "o", "u"]
count = 0
for each character in text:
lowerChar = character.toLowerCase()
if lowerChar in vowels:
count = count + 1
return count
// Usage
sentence = "Hello World"
vowelCount = countVowels(sentence)
print("Vowels found: " + vowelCount) // Vowels found: 3
Problem-Solving Strategies
1. Brute Force
Try all possible solutions until you find the right one.
- Pros: Simple to understand and implement
- Cons: Can be slow for large problems
- When to use: Small problems or when you need a quick solution
2. Divide and Conquer
Break the problem into smaller pieces, solve each piece, then combine results.
- Example: Sorting a large list by splitting it in half repeatedly
- Pros: Often more efficient than brute force
- Cons: Can be more complex to implement
3. Greedy Approach
Make the best choice at each step, hoping it leads to the best overall solution.
- Example: Making change with coins - always use the largest coin possible
- Pros: Simple and often efficient
- Cons: Doesn't always give the optimal solution
Real-World Problem Solving
Problem: Find the Highest Grade
// Problem: Find student with highest grade
function findTopStudent(students):
if length(students) == 0:
return null
topStudent = students[0] // Start with first student
for i = 1 to length(students) - 1:
if students[i].grade > topStudent.grade:
topStudent = students[i]
return topStudent
// Usage
students = [
{name: "Alice", grade: 85},
{name: "Bob", grade: 92},
{name: "Charlie", grade: 78},
{name: "Diana", grade: 96}
]
winner = findTopStudent(students)
print("Top student: " + winner.name + " with grade " + winner.grade)
Problem: Password Validation
function isValidPassword(password):
// Check minimum length
if length(password) < 8:
return false
hasUpper = false
hasLower = false
hasNumber = false
hasSpecial = false
specialChars = "!@#$%^&*"
for each char in password:
if char >= "A" and char <= "Z":
hasUpper = true
else if char >= "a" and char <= "z":
hasLower = true
else if char >= "0" and char <= "9":
hasNumber = true
else if char in specialChars:
hasSpecial = true
return hasUpper and hasLower and hasNumber and hasSpecial
🎯 Algorithm Practice Problems
- Sum Calculator: Write an algorithm to add all numbers in a list
- Word Counter: Count how many times a specific word appears in text
- Temperature Converter: Convert between Celsius and Fahrenheit
- Grade Calculator: Calculate letter grade from percentage score
- Palindrome Checker: Check if a word reads the same forwards and backwards
Algorithm Efficiency
Some algorithms are faster than others:
- Time Complexity: How long does it take to run?
- Space Complexity: How much memory does it use?
Common Time Complexities (from fastest to slowest):
- O(1) - Constant: Same time regardless of input size
- O(log n) - Logarithmic: Time increases slowly
- O(n) - Linear: Time increases proportionally
- O(n²) - Quadratic: Time increases rapidly
Debugging Algorithms
- Trace through by hand: Follow your algorithm step by step
- Use print statements: See what's happening at each step
- Test edge cases: Empty inputs, single items, very large inputs
- Start simple: Test with small, easy examples first