Diagram Desk

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

StartRead sorted array andtarget xlow ← 0, high ← n - 1low ≤ high ?Print not foundEndmid ← (low + high) / 2A[mid] = x ?Print position midA[mid] < x ?low ← mid + 1high ← mid - 1noyesyesnoyesno
Flowchart · 12 shapes

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

ShapeSymbolConnections
StartTerminator→ Read sorted array and target x
Read sorted array and target xInput/output→ low ← 0, high ← n - 1 · ← Start
low ← 0, high ← n - 1Process→ 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 foundInput/output→ End · ← low ≤ high ?
EndTerminator← Print not found, Print position mid
mid ← (low + high) / 2Process→ A[mid] = x ? · ← low ≤ high ?
A[mid] = x ?Decision→ Print position mid(yes), A[mid] < x ?(no) · ← mid ← (low + high) / 2
Print position midInput/output→ End · ← A[mid] = x ?
A[mid] < x ?Decision→ low ← mid + 1(yes), high ← mid - 1(no) · ← A[mid] = x ?
low ← mid + 1Process→ low ≤ high ? · ← A[mid] < x ?
high ← mid - 1Process→ low ≤ high ? · ← A[mid] < x ?

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.

# 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

More flowchart examples

All SeewolLabs tools