Programming · Common Algorithms
up to +100 XP
Stop 2.11 · Programming · 3 min

Common Algorithms

Master fundamental algorithms and their applications

Learning objectives:
  • Know how linear search, binary search, bubble sort, insertion sort and merge sort work (2.11.1–2.11.2)
  • Know their benefits and drawbacks (2.11.3)
  • Compare algorithms by memory, execution time and comparisons (2.11.4)
  • Know best, worst and average cases (2.11.5)
  • Choose a suitable algorithm (2.11.6–2.11.7)

Searching

Linear search

Check each item in turn from the start until the target is found or the list ends.

def linear_search(items, target):
    for i in range(len(items)):
        if items[i] == target:
            return i
    return -1

Binary search

Only works on sorted data. Check the middle item; if it is not the target, discard the half that cannot contain it and repeat on the other half.

Sorting

Bubble sort

Compare each pair of neighbouring items and swap them if they are in the wrong order. Repeat passes until a pass makes no swaps.

Insertion sort

Take each item in turn and insert it into the correct place among the items already sorted to its left.

Merge sort

Split the list in half again and again until each part has one item, then merge the parts back together in order.

# Trace a bubble sort pass on [5, 2, 4, 1]
# compare 5,2 -> swap -> [2, 5, 4, 1]
# compare 5,4 -> swap -> [2, 4, 5, 1]
# compare 5,1 -> swap -> [2, 4, 1, 5]   (largest is now at the end)

Comparing algorithms

Big O notation is not required. Explain cases using logical reasoning instead.

Choosing an algorithm

SituationGood choiceWhy
Small or unsorted list, search onceLinear searchNo need to sort first
Large sorted list, many searchesBinary searchHalves the data each step
Small or nearly sorted listInsertion sortFew moves needed, little memory
Large list, speed mattersMerge sortFast whatever the order, if memory allows
Teaching or tiny listsBubble sortEasiest to understand
Quick check · 15 questions · up to 100 XP

Check what stuck.

+10 XP each
Question 1

Question 1 of 15

Which search only works on sorted data?

Question 2

Question 2 of 15

What is the best case for a linear search?

Question 3

Question 3 of 15

How does a binary search reduce the data each step?

Question 4

Question 4 of 15

Which sort compares neighbouring pairs and swaps them if they are in the wrong order?

Question 5

Question 5 of 15

Which sort takes each item and puts it into the correct place among the items already sorted?

Question 6

Question 6 of 15

Which sort repeatedly splits the list in half then merges the parts in order?

Question 7

Question 7 of 15

Which sort needs the most extra memory?

Question 8

Question 8 of 15

Which is NOT a metric used in the specification to compare algorithms?

Question 9

Question 9 of 15

What is the best case for a bubble sort?

Question 10

Question 10 of 15

A list of 10 unsorted names will be searched once. Which search is most suitable?

Question 11

Question 11 of 15

After one bubble sort pass on [5, 2, 4, 1], what is the list?

Question 12

Question 12 of 15 · Match the pairs

Match each algorithm to how it works.

Linear search
Binary search
Bubble sort
Merge sort
Question 13

Question 13 of 15 · Fill in the blanks

Binary search only works on a list.

Question 14

Question 14 of 15 · Coding

Write linear_search(items, target) that returns the index of target, or -1 if it is not there.

Open the IDE
Hint

Loop through range(len(items)) and compare each item.

Question 15

Question 15 of 15 · Coding

Write bubble_sort(items) that returns the list sorted smallest to largest without using sort().

Open the IDE
Hint

Repeat passes, swapping items[i] and items[i+1] when they are in the wrong order.