Flowchart examples · Programming and algorithms

Binary search flowchart

Look at the middle of a sorted array, throw away the half that cannot hold the target, and repeat until you find it or the range is empty.

Use this template Describe your own version

Binary search in a sorted array

Binary search in a sorted arrayYesNoYesNoYesNoStartRead sorted array A of nitems and target xlow = 0, high = n - 1low ≤ high?Print not foundmid = (low + high) div 2A[mid] = x?Print found at index midA[mid] < x?low = mid + 1high = mid - 1End
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 sorted array A of n items and target x"/]
  init["low = 0, high = n - 1"]
  d1{"low ≤ high?"}
  nf[/"Print not found"/]
  mid["mid = (low + high) div 2"]
  d2{"A[mid] = x?"}
  found[/"Print found at index mid"/]
  d3{"A[mid] < x?"}
  right["low = mid + 1"]
  left["high = mid - 1"]
  e(["End"])
  s --> in
  in --> init
  init --> d1
  d1 -->|Yes| mid
  d1 -->|No| nf
  mid --> d2
  d2 -->|Yes| found
  d2 -->|No| d3
  d3 -->|Yes| right
  d3 -->|No| left
  right --> d1
  left --> d1
  found --> e
  nf --> e

Paste into any Markdown tool that renders Mermaid, such as GitHub.

About this binary search flowchart

Binary search finds a value in a sorted array by repeatedly halving the part of the array that could still contain it. It compares the target with the middle element: if they match, it is done; if the target is larger, it can only be in the right half; if smaller, only in the left half.

The flowchart tracks the search range with two indexes, low and high. Each pass computes mid, checks A[mid], and moves low or high past mid. The loop ends in one of two ways: the target is found, or low passes high, which means the range is empty and the target is not in the array.

The array must already be sorted. On unsorted data the chart can throw away the half that holds the target and report not found. If your data is not sorted, use a linear search, or sort first with an algorithm such as the bubble sort flowchart.

Step by step

  1. Input. Read the sorted array A, its length n and the target x.
  2. Set the range. low = 0 and high = n - 1 cover the whole array.
  3. Loop test. While low ≤ high there is still something to search. When low passes high, print not found.
  4. Middle. Compute mid = (low + high) div 2, using whole-number division.
  5. Compare. If A[mid] equals x, print the index and stop.
  6. Halve. If A[mid] < x, search the right half with low = mid + 1; otherwise search the left half with high = mid - 1. Go back to the loop test.

How to make it in flow-chart.io

  1. Start from the example. Press "Use this template" to open this exact binary search 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 binary search?
Read a sorted array and a target, set low and high to the ends, then loop while low ≤ high: compute mid, stop if A[mid] is the target, otherwise move low to mid + 1 or high to mid - 1. If the loop ends, the target is not in the array.
Why must the array be sorted?
Throwing away half the array is only safe if every value on that side is known to be too small or too large. That is true only when the array is sorted.
What is the time complexity of binary search?
O(log n). Each comparison halves the range, so an array of about a million items needs at most about 20 comparisons.
What is the difference between linear and binary search?
Linear search checks every item in order and works on any list. Binary search needs a sorted list but checks far fewer items.
Can I edit this binary search flowchart?
Yes. Press Use this template to open it in the editor with a free account.

Related

Bubble sort flowchartPrime number flowchartFlowchart to find the largest of three numbersFibonacci 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