Gradient

Courses

∇\nabla∇The maths behind AI0/72
π\piπMaths0/84
⋆\star⋆AI for everyone0/10
Γ\GammaΓGame theory0/49
py\texttt{py}pyPython for AI0/15
y^\hat{y}y^​Build your own AI0/15
All courses

Tuition

My tuitionOlympiad trackCompetitions

Practice

ProjectsAsk a questionTrain on a GPUExplorationsMaths StoriesUnsolved problemsPlaygroundScience labsMock testsOlympiad

Reference

GlossaryBlogDiscoveries
Competitions › SOF ICSO › unit 4 of 16

Sorting algorithms

SOF ICSO · AI, Robotics and IT · about 7 minutes

An algorithm is a precise list of steps. Bubble sort compares neighbours and swaps them when they are out of order, again and again, until a full pass makes no swaps.

Watch the bars swap and the biggest bubble to the end.

In this lesson

  1. Bubble sort
  2. Selection and insertion sort
  3. Searching a sorted list
  4. Worked examples
  5. Where marks are lost
  6. Check yourself

1Bubble sort

Bubble sort compares neighbours and swaps them if they are in the wrong order. After the first pass the largest value has ‘bubbled’ to the end. A list of n items needs at most n − 1 passes.

In the worst case it makes n(n − 1)/2 comparisons, so it is slow for big lists.

2Selection and insertion sort

Selection sort finds the smallest item and swaps it to the front, then repeats on the rest. Insertion sort takes each new item and slides it back into its place among the sorted ones, like sorting playing cards in your hand.

Insertion sort is fast when the list is almost sorted already.

3Searching a sorted list

Binary search looks at the middle item and throws away half the list each time. A list of 1000 items needs at most 10 looks, because 2¹⁰ = 1024.

Linear search checks every item and may need all n looks, but works on unsorted lists.

Worked examples

  1. Example 1Show the list 5, 1, 4, 2 after one pass of bubble sort.

    1. 5, 1 → swap: 1, 5, 4, 2.
    2. 5, 4 → swap: 1, 4, 5, 2.
    3. 5, 2 → swap: 1, 4, 2, 5.

    Answer: 1, 4, 2, 5

  2. Example 2At most how many comparisons does binary search need for 100 sorted items?

    1. 2⁶ = 64 < 100 ≤ 128 = 2⁷.
    2. So 7 halvings are enough.

    Answer: 7

Where marks are lost

  • Doing a full sort when the question asks for the list after one pass.
  • Using binary search on an unsorted list.
  • Counting passes instead of comparisons.

Exam tips

  • After the first pass, the largest item is in its final place.
  • Sorting n items with bubble sort can take about n² comparisons.
  • Searching a sorted list is much faster (binary search).

Practice questions

  1. After one bubble-sort pass on [4, 2, 5, 1], the list is…

  2. Binary search on a sorted list of 16 items needs at most about how many checks?

  3. A step-by-step method to solve a problem is called…

More worked examples

Try each one first, then open the working.

  1. How many swaps happen during the first pass?

    029140210337431512
    Show the working

    Start: Go pair by pair, counting each swap.

    After one pass: [29, 10, 37, 31, 12, 40] with 4 swaps.

    Answer: 4

  2. Binary search looks for 9, using mid = (low + high) // 2. How many items are compared before it is found?

    041921431642152563173683794310441145124813511454mid
    Show the working

    Start: Start with low = 0, high = 14.

    3 comparisons: the list is halved each time.

    Answer: 3

  3. Bubble sort without the early-exit check sorts 6 items. How many comparisons does it make?

    Show the working

    Start: Each pass compares one fewer pair.

    (n − 1) + (n − 2) + … + 1 = n(n − 1)/2 = 15.

    Answer: 15

  4. After the first full pass of bubble sort (ascending), what number is at index 2?

    0141923132148514
    Show the working

    Start: Compare neighbours left to right and swap if the left one is bigger.

    After one pass: [9, 14, 21, 8, 14, 31].

    Answer: 21

Practise more

New questions every time, at exam level

How many swaps happen during the first pass?

0161122431746511

Sit a timed SOF ICSO mock test

Tracing a programNext: Series and patterns