Skip to study material
← Question index

QUESTION 30 · Sorting

Compare bubble sort, selection sort and insertion sort.

5 marks

Answer in brief

The three methods all sort by comparisons, but differ in how values are moved and how they behave on nearly sorted input.

Bubble sort

Adjacent values are compared and swapped when out of order. Each pass fixes a rightmost value. With the early-stop flag used here, sorted input takes O(n); average and worst cases take O(n²). Strictly-greater adjacent swaps preserve equal-value order.

Selection sort

Each pass scans for the minimum remaining value and swaps it into the next leftmost position. The scan count remains quadratic even on sorted input: O(n²). It uses relatively few swaps, at most n - 1, but a distant swap can reverse equal keys, so the usual version is not stable.

Insertion sort

Each next key is inserted into a sorted prefix by shifting larger values. It takes O(n) on already sorted input and O(n²) in the worst case. The strict comparison used here preserves equal-key order. It is often useful for small or nearly sorted lists.

Shared space requirement

All three shown implementations sort within the input array and use only a fixed number of extra variables. Their auxiliary space is O(1). This does not mean the input array takes no space; it means no extra storage proportional to n is needed.

Choosing a method

Look at input size, order, required stability and the cost of moving a record. A comparison of only worst-case Big-O values misses practical differences. For much larger lists, other algorithms may be preferable, but they are outside these three basic syllabus methods.

Comparison table

↔ Scroll horizontally on a small screen
Shown sorting implementations
MethodBest timeWorst timeExtra spaceStable
Optimised bubbleO(n)O(n²)O(1)Yes
SelectionO(n²)O(n²)O(1)No
InsertionO(n)O(n²)O(1)Yes

Related exam questions

Related questions in the bubble sort category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • September/October 2021 · Q5(a) · 7 marks

    Explain bubble sort with an example.

    Paper P01 · page 1
  • July 2021 · Q8(b) · 8 marks

    Explain bubble sort and mention its time complexity.

    Paper P04 · page 1

View this category in the exam question bank →