Flowchart examples · Programming and algorithms

Bubble sort flowchart

Two nested loops compare neighbours and swap them when they are out of order. A swapped flag stops early once a pass changes nothing.

Use this template Describe your own version

Bubble sort (ascending)

Bubble sort (ascending)YesNoYesNoYesNoYesNoStartRead n and array Ai = 0i < n - 1?swapped = false, j = 0j < n - 1 - i?A[j] > A[j + 1]?Swap A[j] and A[j + 1],swapped = truej = j + 1swapped?i = i + 1Print sorted AEnd
Use this template →Opens this exact diagram in the editor, every box and arrow editable. Free account; no AI credits used.

A worked example to adapt. Rename steps, add branches and owners to match how your team actually works.

Edit the steps to match your process. The free preview (no sign-up) drafts up to 8 main steps; add branches and the remaining steps in the editor.
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 --> e

Paste 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

  1. Input. Read the number of elements n and the array A.
  2. Outer loop. Start i at 0. While i < n - 1, run one pass.
  3. Start a pass. Set swapped = false and j = 0.
  4. Inner loop. While j < n - 1 - i, compare A[j] with A[j + 1].
  5. Swap. If A[j] is larger, swap the two values and set swapped = true. Then add 1 to j.
  6. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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

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.

Related

Binary search flowchartFlowchart to find the largest of three numbersFactorial flowchartFibonacci series flowchartLinear search flowchartFlowchart symbolsHow to make a flowchartText to flowchart toolAll flowchart examples

Build it from your own words

Describe your process and get an editable diagram. Free to build and edit; Flow Pro adds downloads and unlimited projects.

Start free See pricing