Flowchart Updated 12/2/2026

Bubble Sort Flowchart

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 sortflowchartsortingalgorithmsnested loopprogramming
12 nodes 14 connections

Study guide

Bubble Sort FlowchartStartRead array a of nnumbersSet i = 0i < n - 1?Set j = 0j < n - 1 - i?a[j] > a[j+1]?Swap a[j] and a[j+1]Set j = j + 1Set i = i + 1Print sorted arrayEndYesNoYesNoYesNo
Bubble Sort Flowchart. Source: https://www.chartcraft.dev/examples/bubble-sort-flowchart

What the bubble sort flowchart shows

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

  1. terminator StartOne Start terminator opens the flowchart.
  2. input/output Read array a of n numbersReads the list and its length n. Here n = 4.
  3. process Set i = 0i counts finished passes. It starts at 0.
  4. 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.
  5. process Set j = 0j is the position of the left item in the pair. It resets to 0 at the start of every pass.
  6. 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.
  7. 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.
  8. 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.
  9. process Set j = j + 1Moves one pair to the right and loops back to the inner test.
  10. process Set i = i + 1One pass is finished, and one more value is in its final place.
  11. input/output Print sorted arrayShows the sorted list once the outer loop ends.
  12. 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.

ComparisonijPair comparedLeft > right?ActionList afterwards
1005 and 2YesSwap[2, 5, 4, 1]
2015 and 4YesSwap[2, 4, 5, 1]
3025 and 1YesSwap[2, 4, 1, 5]
4102 and 4NoNo swap[2, 4, 1, 5]
5114 and 1YesSwap[2, 1, 4, 5]
6202 and 1YesSwap[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.

Flowchart symbols used and why

SymbolShape to draw on paperWhere it appears hereWhy
TerminatorRounded boxStart and EndOne entry, one exit.
Input/outputParallelogramRead array and Print sorted arrayData in and data out.
ProcessRectangleSet i, Set j, Swap, and both updatesEach one changes stored values and has one exit.
DecisionDiamondi < n - 1?, j < n - 1 - i?, a[j] > a[j+1]?Two loop tests and one comparison, each with labelled exits.
LoopA decision whose flow line returns to an earlier stepThe arrows back to j < n - 1 - i? and to i < n - 1?An inner loop of comparisons sits inside an outer loop of passes.
Flow lineArrowEvery arrowShows the order of steps.

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.

How can I use this 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.

Try the studio with your own description.

Type any system, process, or schema in plain English. ChartCraft will draw it in seconds.