Diagram Desk

Bubble sort flowchart

Bubble sort as two nested loops; the core is comparing neighbours and swapping. Shows a decision inside a loop inside a loop.

The diagram

StartRead array A of lengthni ← 0i < n - 1 ?Print sorted AEndj ← 0j < n - 1 - i ?i ← i + 1A[j] > A[j+1] ?Swap the twoj ← j + 1noyesnoyesyesno
Flowchart · 12 shapes

Reading it step by step

  • 1. Start at 'Start'.
  • 2. 'Read array A of length n' is an input or output. Next: 'i ← 0'.
  • 3. 'i ← 0' is a processing step. Next: 'i < n - 1 ?'.
  • 4. Decide 'i < n - 1 ?': if 'no', go to 'Print sorted A'; if 'yes', go to 'j ← 0'.
  • 5. 'Print sorted A' is an input or output. Next: 'End'.
  • 6. End at 'End'.
  • 7. 'j ← 0' is a processing step. Next: 'j < n - 1 - i ?'.
  • 8. Decide 'j < n - 1 - i ?': if 'no', go to 'i ← i + 1'; if 'yes', go to 'A[j] > A[j+1] ?'.
  • 9. 'i ← i + 1' is a processing step. Next: 'i < n - 1 ?'.
  • 10. Decide 'A[j] > A[j+1] ?': if 'yes', go to 'Swap the two'; if 'no', go to 'j ← j + 1'.
  • 11. 'Swap the two' is a processing step. Next: 'j ← j + 1'.
  • 12. 'j ← j + 1' is a processing step. Next: 'j < n - 1 - i ?'.

Shape by shape

ShapeSymbolConnections
StartTerminator→ Read array A of length n
Read array A of length nInput/output→ i ← 0 · ← Start
i ← 0Process→ i < n - 1 ? · ← Read array A of length n
i < n - 1 ?Decision→ Print sorted A(no), j ← 0(yes) · ← i ← 0, i ← i + 1
Print sorted AInput/output→ End · ← i < n - 1 ?
EndTerminator← Print sorted A
j ← 0Process→ j < n - 1 - i ? · ← i < n - 1 ?
j < n - 1 - i ?Decision→ i ← i + 1(no), A[j] > A[j+1] ?(yes) · ← j ← 0, j ← j + 1
i ← i + 1Process→ i < n - 1 ? · ← j < n - 1 - i ?
A[j] > A[j+1] ?Decision→ Swap the two(yes), j ← j + 1(no) · ← j < n - 1 - i ?
Swap the twoProcess→ j ← j + 1 · ← A[j] > A[j+1] ?
j ← j + 1Process→ j < n - 1 - i ? · ← Swap the two, A[j] > A[j+1] ?

Symbols used here

SymbolMeaning
TerminatorWhere the flow starts or ends; a rounded rectangle.
ProcessOne action such as a calculation; a rectangle.
DecisionA question whose answer picks the next path; a diamond.
Input/outputData comes in or goes out; a parallelogram.
Flow lineThe order of steps, in the arrow direction.

All flowchart symbols and how to draw

Diagram text

Paste this text into the editor to get the same picture, then change the names to fit your own work.

# Bubble sort flowchart
flowchart TD
  s(["Start"])
  a[/"Read array A of length n"/]
  b["i ← 0"]
  c{"i < n - 1 ?"}
  k[/"Print sorted A"/]
  z(["End"])
  d["j ← 0"]
  e{"j < n - 1 - i ?"}
  j["i ← i + 1"]
  f{"A[j] > A[j+1] ?"}
  g["Swap the two"]
  h["j ← j + 1"]
  s --> a
  a --> b
  b --> c
  c -->|no| k
  k --> z
  c -->|yes| d
  d --> e
  e -->|no| j
  j --> c
  e -->|yes| f
  f -->|yes| g
  g --> h
  f -->|no| h
  h --> e

More flowchart examples

All SeewolLabs tools