15
Lesson 27 of 44

Complexity and efficiency

Learn to write programs that stay fast as the data grows. In this lesson you will see what complexity and Big O notation are, how to measure time with timeit, how to choose the right data structure, how to compare searches and sorts, and how to speed up an algorithm with memoization and dynamic programming.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

What is complexity and Big O notation

Two programs can give the same answer and yet one can take a second and the other a year. The difference is almost never how fast…

Theory
Read more

Measuring the running time

Theory says what should happen; measuring confirms what really happens. Python includes simple tools to time code, although they…

Theory
Read more

Choosing the right structure

Often the algorithm is the same and what changes the speed is where you store the data . Knowing the cost of each structure's…

Theory
Read more

Efficient searching and sorting

Searching and sorting are the two most repeated tasks in programming, and they are the best showcase for seeing the difference…

Theory
Read more

Memoization and dynamic programming

Sometimes an algorithm is slow because it repeats the same computation again and again. The solution is to save the result the…

Theory
Read more

Practice exercises 10

Growth table

Create a Python program that counts the steps of four kinds of algorithm for several input sizes and shows how each one grows…

Exercise Medium
Solve it

Linear search counting steps

Create a Python program that performs a linear search in a list and counts how many comparisons it needs. In the worst case it is…

Exercise Medium
Solve it

Binary search counting steps

Create a Python program that performs a binary search in a sorted list and compares how many steps it needs against linear…

Exercise Hard
Solve it

First duplicate

Create a Python program that finds the first repeated element of a list. With two nested loops it is O(n²); with a set ( set ) it…

Exercise Medium
Solve it

Two-number sum

Create a Python program that finds two different positions of a list whose values add up to a target . With two loops it is…

Exercise Hard
Solve it

Efficient intersection

Create a Python program that finds the elements common to two lists and compares the cost of doing it with nested loops (O(n ·…

Exercise Medium
Solve it

Bubble sort counting steps

Create a Python program that sorts a list with the bubble sort algorithm and counts its comparisons and swaps. It is an O(n²)…

Exercise Hard
Solve it

Maximum subarray

Create a Python program that finds the largest sum of consecutive elements of a list, and compares an O(n²) brute-force solution…

Exercise Hard
Solve it

Fibonacci with memoization

Create a Python program that computes the Fibonacci number in two ways and compares how many calls each one makes. The simple…

Exercise Hard
Solve it

Coin change

Create a Python program that computes the fewest coins needed to pay an amount with dynamic programming , and compares it with…

Exercise Hard
Solve it