levo

Algorithms

Bubble sort

  • 1 thing to know
  • Component 02

Overview

Recall questions on how bubble sort works and why it is named so, tracing passes, shortening passes, the swap flag optimisation, best, average and worst-case and space complexity, comparison counts, and why it is slower than insertion sort.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Trace a bubble sort on the list 5, 1, 4, 2, showing each pass.

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Describe how a bubble sort works, step by step.

    Model answer

    Start at the beginning of the list and compare the first two items; if they are in the wrong order, swap them. Move along one position and compare the next pair, continuing to the end of the list. That is one pass, and it guarantees the largest remaining item has been carried to the end. Repeat the passes over the unsorted part of the list until a whole pass is completed with no swaps, at which point the list is sorted.

  2. 2

    Trace a bubble sort on the list 5, 1, 4, 2, showing each pass.

    Model answer

    Pass 1: compare 5 and 1 and swap, giving 1, 5, 4, 2; compare 5 and 4 and swap, giving 1, 4, 5, 2; compare 5 and 2 and swap, giving 1, 4, 2, 5. Pass 2: compare 1 and 4, no swap; compare 4 and 2 and swap, giving 1, 2, 4, 5. Pass 3: with the bound shrinking again only 1 and 2 are compared, no swap is made, and because a whole pass passed without a swap the sort stops. The sorted list is 1, 2, 4, 5.

  3. 3

    After one complete pass of a bubble sort, what can you be certain of?

    Model answer

    That the largest value in the list is now in its final position at the end. Any comparison it takes part in results in a swap, so once the pass reaches it, it is carried all the way to the end. Nothing else is guaranteed to be in its final place.

  4. 4

    Why can each pass of a bubble sort be shorter than the one before?

    Model answer

    Because each completed pass fixes one more item at the end of the list. After k passes the last k items are known to be correct, so the next pass need only run over the first n minus k items, saving a growing number of pointless comparisons.

  5. 5

    Describe the optimisation that lets a bubble sort stop early.

    Model answer

    Keep a Boolean flag that is set to false at the start of every pass and set to true whenever a swap is made. At the end of the pass, if no swap was made, then no pair was out of order, so the whole list must be sorted and the algorithm stops immediately instead of completing the remaining passes.

  6. 6

    What is the best-case complexity of bubble sort, and what exactly does it depend on?

    Model answer

    O(n), but only for the optimised version that uses a swap flag, and only when the list is already sorted. That version makes a single pass of n minus 1 comparisons, finds no swaps and stops. Without the flag the algorithm has no way of knowing the list is sorted and grinds through every pass anyway, making even the best case O(n squared).

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Algorithms, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.