11. Algorithms & Problem Solving
Thinking like a programmer
An algorithm is a finite, step-by-step procedure for solving a specific problem. The word sounds intimidating, but you use algorithms every day without realizing it. The steps to make jollof rice is an algorithm. The way you sort your laundry is an algorithm. Getting dressed in the morning — algorithm.
In programming, algorithms are the core logic of any piece of software. Google's search results, Spotify's recommendations, your phone's navigation — all algorithms. Learning to think algorithmically is the most transferable skill in all of programming.
Pseudocode & Flowcharts
Before writing code, great programmers often plan in pseudocode — plain language descriptions of the logic, written in a code-like style without worrying about syntax:
# Pseudocode: Find the largest number in a list
SET largest = first item in list
FOR each item in list:
IF item > largest:
SET largest = item
RETURN largest
Once your pseudocode is solid, translating it to actual code is straightforward. This workflow — think first, code second — saves enormous amounts of debugging time.
Linear Search
The simplest search algorithm: check each item one by one until you find what you're looking for (or run out of items):
def linear_search(items, target): for index, item in enumerate(items): if item == target: return index # Found it! Return position return -1 # Not found students = ['Ama', 'Kofi', 'Abena', 'Kweku', 'Adwoa'] result = linear_search(students, 'Abena') if result != -1: print(f'Found at position {result}') # Found at position 2 else: print('Not found')
Linear search works on any list, sorted or not. In the worst case, it checks every single item. For small lists this is fine — for large datasets, we use more efficient algorithms like binary search.
Bubble Sort
Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they're in the wrong order. Larger elements bubble to the end:
def bubble_sort(arr): n = len(arr) for i in range(n): swapped = False for j in range(0, n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] # Swap swapped = True if not swapped: # Already sorted — early exit break return arr scores = [64, 34, 25, 12, 22, 11, 90] print(bubble_sort(scores)) # [11, 12, 22, 25, 34, 64, 90]
Bubble sort is not used in production (it's slow for large data), but it's the perfect teaching algorithm because its logic is transparent and easy to follow. Real code uses Python's built-in sorted() or .sort(), which uses a sophisticated algorithm called Timsort.
Introduction to Time & Space Complexity
When an algorithm runs faster or slower depending on input size, we describe that relationship using Big O notation. It's a way of asking: as the data gets bigger, how does performance change?
- O(1) — Constant time: same speed regardless of data size. Accessing a list by index.
- O(n) — Linear time: speed grows proportionally with data. Linear search.
- O(n²) — Quadratic time: speed grows with the square of the data. Bubble sort.
- O(log n) — Logarithmic time: speed grows very slowly. Binary search.
An O(n²) algorithm on 10 items takes 100 operations. On 1,000 items: 1,000,000 operations. On 1,000,000 items: it may take hours. Choosing the right algorithm isn't academic — it's the difference between a fast app and a dead one.
Knowledge Check
Ready to test your understanding of 11. Algorithms & Problem Solving?