Bubble sort flowchart, pseudocode and trace table
This flowchart shows bubble sort. The algorithm repeatedly compares neighbouring items in a list and swaps them until a full pass makes no swaps. It is written for GCSE, A-level and AP CSP students and teachers who need to read, write or trace the algorithm in an exam.

Example: Flowchart for bubble sort: click to open it in the editor and change it.
How the flowchart works, step by step
- Start, then set n = length of list.
- Set swapped = True so that the first pass is guaranteed to run.
- Decision swapped = True? If No, go to Print list and Stop. If Yes, begin a new pass.
- Set swapped = False, i = 0. This resets the flag and starts at the first item.
- Decision i < n − 1? If No, the pass is finished, so go back to swapped = True?.
- If Yes, check list[i] > list[i + 1]?. If the pair is out of order, Swap list[i] and list[i + 1], swapped = True.
- Either way, do i = i + 1 and return to i < n − 1?.
The flowchart combines all three programming constructs. Sequence covers the setup boxes. Iteration has an outer loop controlled by the swapped flag and an inner loop controlled by i. Selection is the comparison diamond.
Exam-style pseudocode (OCR Exam Reference Language)
// list already holds the data to sort
n = list.length
swapped = True
while swapped == True
swapped = False
i = 0
while i < n - 1
if list[i] > list[i + 1] then
temp = list[i]
list[i] = list[i + 1]
list[i + 1] = temp
swapped = True
endif
i = i + 1
endwhile
endwhile
print(list)Trace table for list = [5, 1, 4, 2]
Before the first pass, the variables are n = 4 and swapped = True. Each row below shows the values at the end of one pass, when i < n − 1? gives No.
| Pass | n | i | swapped | list | Output |
|---|---|---|---|---|---|
| 1 | 4 | 3 | True | [1, 4, 2, 5] | |
| 2 | 4 | 3 | True | [1, 2, 4, 5] | |
| 3 | 4 | 3 | False | [1, 2, 4, 5] | [1, 2, 4, 5] |
Pass 1 makes three swaps (5↔1, 5↔4, 5↔2). Pass 2 makes one swap (4↔2). Pass 3 makes no swaps, so swapped stays False and the list is printed.
How to adapt it for your project
- Descending order: change the comparison to list[i] < list[i + 1].
- Strings: the same comparison sorts names alphabetically in most languages.
- Efficiency: add n = n − 1 after each pass, because the largest item has already bubbled to the end.
- User input: add an input loop before n = length of list that stops at a rogue value such as -1. Use validation to reject non-numbers.
Tip: A common exam mistake is swapping without a temporary variable. If you write list[i] = list[i + 1] and then list[i + 1] = list[i], both positions end up holding the same value. Always copy one value into temp first.
Frequently asked questions
How does bubble sort know when to stop?
It uses a Boolean flag called swapped, which is reset to False at the start of every pass. If a whole pass makes no swaps, the list is sorted and the outer loop ends.
Why does the loop use i < n − 1 instead of i < n?
Each comparison looks at list[i + 1]. If i reached n − 1, the algorithm would try to read past the end of the list and cause an index error.
How many passes does bubble sort need?
With the swapped flag, an already sorted list needs just 1 pass. In the worst case, a reversed list of n items needs n passes: n − 1 passes to sort it plus one final pass with no swaps.
Make your own flowchart with AI →