No history yet

Introduction to Algorithm Analysis

More Than One Way to Solve a Problem

An algorithm is simply a set of instructions for completing a task. If you've ever followed a recipe to bake a cake or used a map to find a new coffee shop, you've used an algorithm. It's a step-by-step guide to get from a starting point to a goal.

For almost any problem, there are multiple ways to solve it. Imagine you're looking for a friend's name in a massive, old-school phone book. You could start at the very first page and scan every single name until you find the right one. That's one algorithm. It's simple, but not very smart.

Alternatively, you could open the book to the middle. If your friend's name, say "Smith," comes after the names on that page, you know you only need to search the second half of the book. You can repeat this process, dividing the problem in half each time. This is a different, much faster algorithm.

Measuring Efficiency

When we talk about one algorithm being "better" than another, we're usually talking about its efficiency. How many resources does it consume to get the job done? In computer science, we care about two main resources: time and memory.

The analysis of an algorithm's efficiency is called algorithm complexity.

This breaks down into two key areas:

Time Complexity

noun

A measure of how much time an algorithm takes to run, as a function of the length of its input.

This isn't about seconds or minutes on a specific computer. It’s about how the number of operations grows as the input size increases. Does it double? Does it quadruple? Does it barely change at all?

Space Complexity

noun

A measure of the amount of memory an algorithm needs to run, as a function of the length of its input.

This is about how much working memory, or RAM, the algorithm requires. Some algorithms need to store a lot of intermediate data, while others are more streamlined.

Lesson image

Why does this matter? For a small phone book, the page-by-page method might be fine. But what if you're Google, and your "phone book" is the entire internet? An inefficient algorithm could take years and require a warehouse full of computers. An efficient one can find what you need in a fraction of a second on your phone. Analyzing algorithms helps us choose the right tool for the job, saving time, energy, and money.

Thinking Big with Asymptotic Analysis

To compare algorithms fairly, we need a consistent method. It wouldn't make sense to measure one algorithm on a supercomputer and another on a ten-year-old laptop and declare the first one faster. The speed of the hardware, the programming language, and even the compiler can affect the actual runtime.

To get around this, we use a technique called asymptotic analysis. It's a way of looking at the performance of an algorithm as the input size grows very, very large—approaching infinity.

Asymptotic analysis focuses on the growth rate of an algorithm's resource use, not the exact numbers. It describes the algorithm's long-term behavior.

By focusing on how the number of operations scales with the input size, we can ignore the specifics of the hardware and implementation. We're not interested in whether an algorithm takes 5 seconds or 5.2 seconds for a list of 100 items. We want to know what happens when that list grows to a million or a billion items. Does the runtime explode, or does it grow at a manageable pace? This approach gives us a high-level understanding of an algorithm's inherent efficiency.