Binary search flowchart
Binary search halves the range by comparing with the middle element. Shows how three outcomes (equal, less, greater) become two decisions.
The diagram
Reading it step by step
- 1. Start at 'Start'.
- 2. 'Read sorted array and target x' is an input or output. Next: 'low ← 0, high ← n - 1'.
- 3. 'low ← 0, high ← n - 1' is a processing step. Next: 'low ≤ high ?'.
- 4. Decide 'low ≤ high ?': if 'no', go to 'Print not found'; if 'yes', go to 'mid ← (low + high) / 2'.
- 5. 'Print not found' is an input or output. Next: 'End'.
- 6. End at 'End'.
- 7. 'mid ← (low + high) / 2' is a processing step. Next: 'A[mid] = x ?'.
- 8. Decide 'A[mid] = x ?': if 'yes', go to 'Print position mid'; if 'no', go to 'A[mid] < x ?'.
- 9. 'Print position mid' is an input or output. Next: 'End'.
- 10. Decide 'A[mid] < x ?': if 'yes', go to 'low ← mid + 1'; if 'no', go to 'high ← mid - 1'.
- 11. 'low ← mid + 1' is a processing step. Next: 'low ≤ high ?'.
- 12. 'high ← mid - 1' is a processing step. Next: 'low ≤ high ?'.
Shape by shape
| Shape | Symbol | Connections |
|---|---|---|
| Start | Terminator | → Read sorted array and target x |
| Read sorted array and target x | Input/output | → low ← 0, high ← n - 1 · ← Start |
| low ← 0, high ← n - 1 | Process | → low ≤ high ? · ← Read sorted array and target x |
| low ≤ high ? | Decision | → Print not found(no), mid ← (low + high) / 2(yes) · ← low ← 0, high ← n - 1, low ← mid + 1, high ← mid - 1 |
| Print not found | Input/output | → End · ← low ≤ high ? |
| End | Terminator | ← Print not found, Print position mid |
| mid ← (low + high) / 2 | Process | → A[mid] = x ? · ← low ≤ high ? |
| A[mid] = x ? | Decision | → Print position mid(yes), A[mid] < x ?(no) · ← mid ← (low + high) / 2 |
| Print position mid | Input/output | → End · ← A[mid] = x ? |
| A[mid] < x ? | Decision | → low ← mid + 1(yes), high ← mid - 1(no) · ← A[mid] = x ? |
| low ← mid + 1 | Process | → low ≤ high ? · ← A[mid] < x ? |
| high ← mid - 1 | Process | → low ≤ high ? · ← A[mid] < x ? |
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.
# Binary search flowchart
flowchart TD
s(["Start"])
a[/"Read sorted array and target x"/]
b["low ← 0, high ← n - 1"]
c{"low ≤ high ?"}
g[/"Print not found"/]
z(["End"])
d["mid ← (low + high) / 2"]
e{"A[mid] = x ?"}
f[/"Print position mid"/]
h{"A[mid] < x ?"}
i["low ← mid + 1"]
j["high ← mid - 1"]
s --> a
a --> b
b --> c
c -->|no| g
g --> z
c -->|yes| d
d --> e
e -->|yes| f
f --> z
e -->|no| h
h -->|yes| i
h -->|no| j
i --> c
j --> c