22
Lesson 13 of 44

Classic algorithms

Classic algorithms are the recipes every programmer should know. In this lesson you will learn linear and binary search, the bubble, selection and insertion sorting algorithms, the Euclidean algorithm, the sieve of Eratosthenes, memoization and the two-pointer technique, and you will discover how to compare their efficiency.

  • 6 theory articles
  • 16 practice exercises
Start lesson

Fundamentals concepts 6

Linear and binary search

An algorithm is a step-by-step recipe to solve a problem. Some are so useful that they are called classic . The most basic one is…

Theory
Read more

Sorting algorithms

Sorting is another classic problem. Python already includes sorted() and list.sort() , but knowing the basic algorithms teaches…

Theory
Read more

Number algorithms: Euclid and the sieve

Numbers also have their classic algorithms, some of them more than 2000 years old.

Theory
Read more

Memoization, two pointers and efficiency

Recursive Fibonacci recalculates the same values over and over again:

Theory
Read more

Divide and conquer: merge sort and quicksort

The bubble, selection and insertion sorting algorithms take a time proportional to n² : with 100,000 elements that is billions of…

Theory
Read more

Linear algorithms: counting, quickselect and Boyer-Moore

You do not always need to sort. Three classic algorithms solve with a single pass (or almost) problems that seem to ask for…

Theory
Read more

Practice exercises 16

Linear search

Create a Python program that looks for a number in a list by going through it from start to end (linear search).

Exercise Easy
Solve it

Minimum and maximum by hand

Create a Python program that finds the smallest and the largest value of a list by going through it only once.

Exercise Easy
Solve it

Binary search

Create a Python program that looks for a number in a sorted list using binary search and counts how many steps it needs.

Exercise Medium
Solve it

Bubble sort (classic algorithm)

Create a Python program that sorts a list with the bubble sort algorithm and shows how it looks after each pass.

Exercise Medium
Solve it

Selection sort

Create a Python program that sorts a list with the selection sort algorithm and shows the list after each step.

Exercise Medium
Solve it

Insertion sort

Create a Python program that sorts a list with the insertion sort algorithm and counts the shifts it performs.

Exercise Medium
Solve it

Greatest common divisor

Create a Python program that computes the greatest common divisor of two numbers with the Euclidean algorithm.

Exercise Easy
Solve it

Sieve of Eratosthenes

Create a Python program that gets all the prime numbers up to n with the sieve of Eratosthenes.

Exercise Medium
Solve it

Fibonacci with memory

Create a Python program that computes the Fibonacci number n with recursion and a memory (dictionary) that avoids repeating…

Exercise Medium
Solve it

Pair with a given sum

Create a Python program that finds two numbers in a sorted list that add up to a given value, using the two-pointer technique.

Exercise Medium
Solve it

Merge sort

Merge sort splits the list into two halves, sorts each one recursively and merges them into a sorted list. It is one of the…

Exercise Hard
Solve it

Quicksort

Quicksort chooses a pivot , separates the other elements into smaller and larger than it, and sorts each group recursively. It is…

Exercise Hard
Solve it

Counting sort

When the values are small integers, there is no need to compare: you count how many times each value appears and rebuild the…

Exercise Medium
Solve it

The k-th smallest

To find the k-th smallest of a list (for example the median) you do not need to sort it entirely. Quickselect uses the idea of…

Exercise Hard
Solve it

Majority element

An element is a majority if it appears in more than half of the positions. The Boyer-Moore algorithm finds it by going through…

Exercise Medium
Solve it

Are they permutations of each other?

Two lists are a permutation of each other if they have exactly the same elements, repeated the same number of times, even in a…

Exercise Easy
Solve it