Bubble sort (ascending)
A worked example to adapt. Rename steps, add branches and owners to match how your team actually works.
Mermaid source for this diagram
flowchart TD
s(["Start"])
in[/"Read n and array A"/]
i0["i = 0"]
d1{"i < n - 1?"}
pass["swapped = false, j = 0"]
d2{"j < n - 1 - i?"}
d3{"A[j] > A[j + 1]?"}
swap["Swap A[j] and A[j + 1], swapped = true"]
incj["j = j + 1"]
d4{"swapped?"}
inci["i = i + 1"]
out[/"Print sorted A"/]
e(["End"])
s --> in
in --> i0
i0 --> d1
d1 -->|Yes| pass
d1 -->|No| out
pass --> d2
d2 -->|Yes| d3
d2 -->|No| d4
d3 -->|Yes| swap
d3 -->|No| incj
swap --> incj
incj --> d2
d4 -->|Yes| inci
d4 -->|No| out
inci --> d1
out --> ePaste into any Markdown tool that renders Mermaid, such as GitHub.
About this bubble sort flowchart
Bubble sort walks through a list, compares each pair of neighbours and swaps them if they are in the wrong order. After the first pass the largest value has moved to the end, like a bubble rising. After the second pass the second largest is in place, and so on.
The flowchart has two loops. The outer loop, counted by i, runs one pass at a time. The inner loop, counted by j, compares A[j] with A[j + 1]. Each pass can stop one position earlier than the last, because the end of the array is already sorted, which is why the inner test is j < n - 1 - i.
The swapped flag is the improvement most courses ask for. If a whole pass makes no swaps, the list is already sorted and the chart jumps straight to the output. That makes bubble sort finish in one pass on a sorted list instead of running every pass.
Step by step
- Input. Read the number of elements n and the array A.
- Outer loop. Start i at 0. While i < n - 1, run one pass.
- Start a pass. Set swapped = false and j = 0.
- Inner loop. While j < n - 1 - i, compare A[j] with A[j + 1].
- Swap. If A[j] is larger, swap the two values and set swapped = true. Then add 1 to j.
- Early exit. When the pass ends, if nothing was swapped the list is sorted: print it. Otherwise add 1 to i and start the next pass.
How to make it in flow-chart.io
- Start from the example. Press "Use this template" to open this exact bubble sort flowchart in the editor, or edit the text in the generator box and press Generate for a free preview without signing up.
- Match your program. Rename the input, process and output boxes so they use the same variable names as your code. Keep one action per box.
- Check every branch. Trace each diamond with a test value for the Yes exit and one for the No exit, and make sure both reach the end oval.
- Share or export. Share a read-only view link with a classmate or reviewer. Downloads as PNG, SVG or PDF are part of Flow Pro (see pricing).
Tips
- Trace A = [5, 1, 4, 2]: pass 1 gives [1, 4, 2, 5], pass 2 gives [1, 2, 4, 5], pass 3 makes no swaps, so the chart prints [1, 2, 4, 5].
- For descending order, change the comparison to A[j] < A[j + 1]; nothing else changes.
- Pseudocode: for i = 0 to n - 2: swapped = false; for j = 0 to n - 2 - i: if A[j] > A[j + 1]: swap; swapped = true; if not swapped: break.
- Bubble sort does about n² / 2 comparisons in the worst case, so it is a teaching algorithm. Real programs use the language's built-in sort.
- Draw the swap as one process box. Showing the temporary variable (temp = A[j], A[j] = A[j + 1], A[j + 1] = temp) is clearer if your course asks for it.
Frequently asked questions
- What is the flowchart for bubble sort?
- Read the array, then run an outer loop of passes and an inner loop that compares each pair of neighbours and swaps them when the left one is larger. A swapped flag ends the sort early when a pass makes no swaps.
- Why does the inner loop stop at n - 1 - i?
- After each pass the largest remaining value is in its final place at the end of the array. After i passes, the last i positions are sorted, so there is no need to compare them again.
- What is the time complexity of bubble sort?
- O(n²) comparisons in the worst and average case. With the swapped flag shown here, the best case on an already sorted list is O(n): one pass with no swaps.
- Is bubble sort stable?
- Yes. It only swaps when the left value is strictly greater, so equal values keep their original order.
- Can I open this bubble sort flowchart in the editor?
- Yes. Press Use this template to open it with a free account and rename the boxes to match your code.