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
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
| Shape | Symbol | Connections |
|---|---|---|
| Start | Terminator | → Read array A of length n |
| Read array A of length n | Input/output | → i ← 0 · ← Start |
| i ← 0 | Process | → 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 A | Input/output | → End · ← i < n - 1 ? |
| End | Terminator | ← Print sorted A |
| j ← 0 | Process | → 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 + 1 | Process | → 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 two | Process | → j ← j + 1 · ← A[j] > A[j+1] ? |
| j ← j + 1 | Process | → j < n - 1 - i ? · ← Swap the two, A[j] > A[j+1] ? |
Symbols used here
| Symbol | Meaning |
|---|---|
| Terminator | Where the flow starts or ends; a rounded rectangle. |
| Process | One action such as a calculation; a rectangle. |
| Decision | A question whose answer picks the next path; a diamond. |
| Input/output | Data comes in or goes out; a parallelogram. |
| Flow line | The order of steps, in the arrow direction. |
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