Flowchart examples · Programming and algorithms

Linear search flowchart (with pseudocode)

Check each element in turn, stop at the first match, and report not found when the list runs out. Traced for a five-item list.

Use this template Describe your own version

Linear search for a key in a list of n items

Linear search for a key in a list of n itemsYesNoYesNoStartRead list A of n items(n ≥ 1) and the keyi = 0A[i] = key?Print key found at indexii = i + 1i < n?Print key not foundEnd
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 list A of n items (n ≥ 1) and the key"/]
  init["i = 0"]
  d2{"A[i] = key?"}
  found[/"Print key found at index i"/]
  inc["i = i + 1"]
  d1{"i < n?"}
  nf[/"Print key not found"/]
  e(["End"])
  s --> in
  in --> init
  init --> d2
  d2 -->|Yes| found
  d2 -->|No| inc
  inc --> d1
  d1 -->|Yes| d2
  d1 -->|No| nf
  found --> e
  nf --> e

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

About this linear search flowchart

Linear search, also called sequential search, looks for a value by checking each element of a list in order, from the first to the last. It works on any list, sorted or not, which is why it is usually the first search algorithm taught.

The flowchart uses two decisions. A[i] = key? stops the search as soon as the key is found. After each miss, i moves to the next position and i < n? checks whether any items are left. If the index runs past the last item without a match, the chart prints not found.

The bounds check comes right after i is increased, so the chart never compares an element past the end of the list, which in many languages would be an error. Because the first comparison happens before any check, the chart assumes the list has at least one item; for a list that may be empty, add an n = 0? check before the loop.

Step by step

  1. Input. Read the list A with n items and the key to look for.
  2. Initialize. Set the index i to 0, the first position in the list.
  3. Compare. If A[i] equals the key, print its position and end.
  4. Move on. Otherwise add 1 to i.
  5. Bounds check. If i is still less than n, go back and compare the next item. If not, every item has been checked: print not found and end.

How to make it in flow-chart.io

  1. Start from the example. Press "Use this template" to open this exact linear 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 linear search?
Start, read the list and the key, set i = 0, then loop: if A[i] equals the key, print i and stop; otherwise add 1 to i, and if i is still less than n compare again. When i reaches n, print not found and stop.
Does linear search need a sorted list?
No. Linear search checks every element in order, so it works on unsorted lists. Binary search is faster but needs the list to be sorted first.
What is the time complexity of linear search?
O(n) in the worst case, when the key is last or missing. In the best case, when the key is the first element, it takes one comparison.
How do I write linear search in Python?
for i in range(len(a)): if a[i] == key: return i, then return -1 after the loop to mean not found.
Can I edit this linear search flowchart?
Yes. Press Use this template to open it in the editor with a free account, or type your own steps into the text to flowchart tool.

Related

Binary search flowchartBubble sort flowchartFlowchart to find the largest of three numbersFibonacci series 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