Bubble sort flowchart: two nested loops compare neighbours and swap them when they are in the wrong order, so the largest value bubbles to the end on each pass. With a trace of [5, 2, 4, 1], pseudocode, and tested Python, C and Java code.
Bubble sort walks along a list comparing each pair of neighbours and swaps them if the left one is bigger, so after each pass the largest unsorted value has bubbled to the end. A bubble sort flowchart is a nested loop with a third diamond inside: the outer loop counts passes, the inner loop walks the pairs, and the inner diamond decides whether to swap.
The trace below sorts [5, 2, 4, 1] into [1, 2, 4, 5] in 6 comparisons.
Bubble sort flowchart, step by step
terminator StartOne Start terminator opens the flowchart.
input/output Read array a of n numbersReads the list and its length n. Here n = 4.
process Set i = 0i counts finished passes. It starts at 0.
decision i < n - 1?A list of n items needs at most n - 1 passes. Yes starts another pass, No means the list is sorted.
process Set j = 0j is the position of the left item in the pair. It resets to 0 at the start of every pass.
decision j < n - 1 - i?Why n - 1 - i? After pass i, the last i items are already in their final places, so each pass can stop one pair earlier.
decision a[j] > a[j+1]?The heart of bubble sort: is the left neighbour bigger than the right? Yes means they are in the wrong order.
process Swap a[j] and a[j+1]Exchanges the two values. In code this needs a temporary variable: temp = a[j], a[j] = a[j+1], a[j+1] = temp.
process Set j = j + 1Moves one pair to the right and loops back to the inner test.
process Set i = i + 1One pass is finished, and one more value is in its final place.
input/output Print sorted arrayShows the sorted list once the outer loop ends.
terminator EndOne End terminator.
Dry run: trace table for a = [5, 2, 4, 1], n = 4
Each row is one comparison. The last column shows the list after that step.
Comparison
i
j
Pair compared
Left > right?
Action
List afterwards
1
0
0
5 and 2
Yes
Swap
[2, 5, 4, 1]
2
0
1
5 and 4
Yes
Swap
[2, 4, 5, 1]
3
0
2
5 and 1
Yes
Swap
[2, 4, 1, 5]
4
1
0
2 and 4
No
No swap
[2, 4, 1, 5]
5
1
1
4 and 1
Yes
Swap
[2, 1, 4, 5]
6
2
0
2 and 1
Yes
Swap
[1, 2, 4, 5]
Pass 0 makes 3 comparisons, pass 1 makes 2, pass 2 makes 1. That is 3 + 2 + 1 = 6, which is n(n - 1) / 2 for n = 4. Watch the 5 ride to the end in the first pass.
Bubble sort pseudocode
Pseudocode
READ a, n
FOR i = 0 TO n - 2
FOR j = 0 TO n - 2 - i
IF a[j] > a[j + 1] THEN
temp = a[j]
a[j] = a[j + 1]
a[j + 1] = temp
ENDIF
NEXT j
NEXT i
PRINT a
Bubble sort in Python, C and Java
Python
n = int(input())
a = [int(x) for x in input().split()]
for i in range(n - 1):
for j in range(n - 1 - i):
if a[j] > a[j + 1]:
temp = a[j]
a[j] = a[j + 1]
a[j + 1] = temp
print(*a)
C
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
int a[100];
for (int k = 0; k < n; k++) scanf("%d", &a[k]);
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int k = 0; k < n; k++) printf(k ? " %d" : "%d", a[k]);
printf("\n");
return 0;
}
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for (int k = 0; k < n; k++) a[k] = in.nextInt();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
StringBuilder out = new StringBuilder();
for (int k = 0; k < n; k++) out.append(k == 0 ? "" : " ").append(a[k]);
System.out.println(out);
}
}
Input typed at the keyboard
4
5 2 4 1
Output of all three programs
1 2 4 5
All three programs were compiled and run with the input shown (n, then the numbers) before publishing, and they make the same 6 comparisons as the trace. The C version holds at most 100 numbers.
Mistakes examiners see most
Swapping without a temporary variable. Writing a[j] = a[j+1] then a[j+1] = a[j] copies the same value twice and loses the other one. You need temp (Python can also write a[j], a[j+1] = a[j+1], a[j]).
Letting j run to n - 1. Then a[j+1] reads past the end of the list. The last pair starts at n - 2, which is why the inner test uses < and subtracts 1.
Putting Set j = 0 outside the outer loop. Like every nested loop, the inner counter must restart on each pass, or only the first pass happens.
Sending the No exit of the swap test to the outer loop. No swap just means this pair is fine. The flow must still move to the next pair (Set j = j + 1).
Practice questions with worked answers
Q1 Write the list after the first pass of bubble sort on [3, 1, 2].
[1, 2, 3].
Compare 3 and 1: swap to [1, 3, 2]. Compare 3 and 2: swap to [1, 2, 3]. The second pass compares 1 and 2 and swaps nothing.
Q2 How many comparisons does this flowchart make for a list of 6 numbers?
15 comparisons.
The passes make 5 + 4 + 3 + 2 + 1 = 15 comparisons, which is 6 * 5 / 2. The number of swaps depends on the data, but the number of comparisons does not.
Q3 Add an early exit: stop as soon as a whole pass makes no swaps. What changes in the flowchart?
Add a flag swapped. Set it to False after Set j = 0, set it to True in the swap box, and on the inner loop's No exit add a diamond swapped?. Yes goes to Set i = i + 1, No goes straight to Print sorted array.
Pseudocode
REPEAT
swapped = FALSE
FOR j = 0 TO n - 2
IF a[j] > a[j + 1] THEN
swap a[j] and a[j + 1]
swapped = TRUE
ENDIF
NEXT j
UNTIL swapped = FALSE
On a list that is already sorted, this version stops after one pass.
In the interactive diagram every step is a labelled card: the small word above each step names its kind (process, decision or data). When you draw this flowchart by hand, use the standard shapes in the table. The full list is on the flowchart symbols page.
Make your own version
Each link opens the editor with a prompt already filled in. Change the wording, then generate your own flowchart.
Download the Bubble Sort Flowchart as a PNG or SVG, or open it in the studio to edit your own copy. To draw a similar flowchart from a description, use the flowchart generator; for notation and a step-by-step walkthrough, read how to make a flowchart; and browse more flowchart examples for related layouts.
Flowchart to check prime number: read n, reject anything below 2, then try every divisor i while i * i <= n. With a trace table for n = 29, pseudocode, and tested Python, C and Java code.
Fibonacci series flowchart: keep the last two terms in a and b, print a, then slide the pair forward with c = a + b. With a trace of the first 7 terms (0 1 1 2 3 5 8), pseudocode, and tested Python, C and Java code.
Flowchart for factorial of a number: start fact at 1, multiply it by every i from 1 to n in a loop, then print it. With a trace for n = 5 (answer 120), pseudocode, tested Python, C and Java code, and the recursive version.
factorialflowchartloopsalgorithmsprogramming
Try the studio with your own description.
Type any system, process, or schema in plain English. ChartCraft will draw it in seconds.