Binary search in a sorted array
A worked example to adapt. Rename steps, add branches and owners to match how your team actually works.
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 --> ePaste 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
- Input. Read the sorted array A, its length n and the target x.
- Set the range. low = 0 and high = n - 1 cover the whole array.
- Loop test. While low ≤ high there is still something to search. When low passes high, print not found.
- Middle. Compute mid = (low + high) div 2, using whole-number division.
- Compare. If A[mid] equals x, print the index and stop.
- 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
- 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.
- 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.
- 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.
- 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
- Trace A = [3, 8, 15, 21, 30, 42], x = 30: mid = 2 (15 < 30, low = 3), mid = 4 (30 found). Two comparisons instead of five.
- Trace x = 10 in the same array: mid = 2 (15 > 10, high = 1), mid = 0 (3 < 10, low = 1), mid = 1 (8 < 10, low = 2), now low > high, so not found.
- Pseudocode: low = 0; high = n - 1; while low ≤ high: mid = (low + high) div 2; if A[mid] = x return mid; if A[mid] < x then low = mid + 1 else high = mid - 1; return not found.
- In languages with fixed-size integers, mid = low + (high - low) div 2 avoids overflow when low and high are very large.
- Use mid + 1 and mid - 1, not mid. Setting low = mid can loop forever when low and high are next to each other.
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.