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…
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.
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…
Sorting is another classic problem. Python already includes sorted() and list.sort() , but knowing the basic algorithms teaches…
Numbers also have their classic algorithms, some of them more than 2000 years old.
Recursive Fibonacci recalculates the same values over and over again:
The bubble, selection and insertion sorting algorithms take a time proportional to n² : with 100,000 elements that is billions of…
You do not always need to sort. Three classic algorithms solve with a single pass (or almost) problems that seem to ask for…
Create a Python program that looks for a number in a list by going through it from start to end (linear search).
Create a Python program that finds the smallest and the largest value of a list by going through it only once.
Create a Python program that looks for a number in a sorted list using binary search and counts how many steps it needs.
Create a Python program that sorts a list with the bubble sort algorithm and shows how it looks after each pass.
Create a Python program that sorts a list with the selection sort algorithm and shows the list after each step.
Create a Python program that sorts a list with the insertion sort algorithm and counts the shifts it performs.
Create a Python program that computes the greatest common divisor of two numbers with the Euclidean algorithm.
Create a Python program that gets all the prime numbers up to n with the sieve of Eratosthenes.
Create a Python program that computes the Fibonacci number n with recursion and a memory (dictionary) that avoids repeating…
Create a Python program that finds two numbers in a sorted list that add up to a given value, using the two-pointer technique.
Merge sort splits the list into two halves, sorts each one recursively and merges them into a sorted list. It is one of the…
Quicksort chooses a pivot , separates the other elements into smaller and larger than it, and sorts each group recursively. It is…
When the values are small integers, there is no need to compare: you count how many times each value appears and rebuild the…
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…
An element is a majority if it appears in more than half of the positions. The Boyer-Moore algorithm finds it by going through…
Two lists are a permutation of each other if they have exactly the same elements, repeated the same number of times, even in a…