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:
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):
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:
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?