Flowchart Updated 12/2/2026

Linear Search Flowchart

Linear search flowchart: check each item of a list from the start until one equals the key, or report Not found when the list runs out. With a trace for key 4 in [7, 3, 9, 4, 6], pseudocode, and tested Python, C and Java code.

linear searchflowchartsearchingalgorithmsprogramming
9 nodes 10 connections

Study guide

Linear Search FlowchartStartRead array a, n andkeySet i = 0i < n?a[i] == key?Set i = i + 1Print "Found atindex", iPrint "Not found"EndYesNoYesNo
Linear Search Flowchart. Source: https://www.chartcraft.dev/examples/linear-search-flowchart

What the linear search flowchart shows

Linear search looks at the items of a list one by one, from the first to the last, until it finds the value it wants (the key) or runs out of items. A linear search flowchart is a loop with two diamonds and two ways out: one for Found, one for Not found.

It works on any list, sorted or not, which is its big advantage over binary search.

Linear search flowchart, step by step

  1. terminator StartOne Start terminator opens the flowchart.
  2. input/output Read array a, n and keyReads the list, its length n and the key to look for.
  3. process Set i = 0i is the index being checked. Lists are counted from 0.
  4. decision i < n?Are there items left? This test must come before reading a[i], or the search would read past the end of the list. No means every item was checked.
  5. decision a[i] == key?Is the current item the key? Yes ends the search early. No moves on.
  6. process Set i = i + 1Moves to the next index and loops back to i < n?.
  7. input/output Print "Found at index", iReports the position. The loop stops here, so later items are never checked.
  8. input/output Print "Not found"Reached only after all n items failed the match test.
  9. terminator EndBoth paths meet at one End.

Dry run: trace table for a = [7, 3, 9, 4, 6], n = 5, key = 4

Each row is one pass of the loop, starting at the test i < n?.

Passii < n?a[i]a[i] == key?Next step
10Yes7NoSet i = i + 1
21Yes3NoSet i = i + 1
32Yes9NoSet i = i + 1
43Yes4YesPrint "Found at index 3", then End

4 comparisons with the key. The 6 at index 4 is never looked at. Searching for 8 instead would take 5 comparisons and one last test 5 < 5? that answers No.

Linear search pseudocode

Pseudocode
READ a, n, key
i = 0
WHILE i < n
    IF a[i] == key THEN
        PRINT "Found at index", i
        STOP
    ENDIF
    i = i + 1
ENDWHILE
PRINT "Not found"

Linear search in Python, C and Java

Python
n = int(input())
a = [int(x) for x in input().split()]
key = int(input())

i = 0
found = False
while i < n:
    if a[i] == key:
        print("Found at index", i)
        found = True
        break
    i = i + 1

if not found:
    print("Not found")
C
#include <stdio.h>

int main(void) {
    int n, key;
    int a[100];
    scanf("%d", &n);
    for (int k = 0; k < n; k++) scanf("%d", &a[k]);
    scanf("%d", &key);

    int i = 0;
    while (i < n) {
        if (a[i] == key) {
            printf("Found at index %d\n", i);
            return 0;
        }
        i = i + 1;
    }
    printf("Not found\n");
    return 0;
}
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for (int k = 0; k < n; k++) a[k] = in.nextInt();
        int key = in.nextInt();

        int i = 0;
        while (i < n) {
            if (a[i] == key) {
                System.out.println("Found at index " + i);
                return;
            }
            i = i + 1;
        }
        System.out.println("Not found");
    }
}
Input typed at the keyboard
5
7 3 9 4 6
4
Output of all three programs
Found at index 3

All three programs were compiled and run with the input shown (n, the list, then the key) before publishing. The C version holds at most 100 numbers.

Mistakes examiners see most

  • Printing Not found inside the loop. If the No exit of a[i] == key? prints Not found, the program says Not found after the very first mismatch. Not found belongs after the loop, on the No exit of i < n?.
  • Testing a[i] before testing i < n. When the key is missing, i reaches n and a[n] is outside the list. Always check that an item exists before you read it.
  • Forgetting the update. Without Set i = i + 1 the flow keeps comparing the same item forever, a classic while loop mistake.

Practice questions with worked answers

Q1 How many comparisons with the key does linear search make, in the worst case, for a list of 50 items?

50 comparisons.

The worst case is when the key is the last item or not in the list at all, so every item is checked once. The best case is 1 comparison, when the key is first.

Q2 The list is [2, 8, 2, 5] and the key is 2. Which index is printed, and why not the other one?

Index 0.

The flowchart stops at the first match. To report every match, the Found box would have to loop back to Set i = i + 1 instead of going to End.

Q3 Change the flowchart so it counts how many times the key appears.

Add Set count = 0 before the loop. On the Yes exit of a[i] == key? add Set count = count + 1 and then go to Set i = i + 1. On the No exit of i < n? print count.

Pseudocode
count = 0
FOR i = 0 TO n - 1
    IF a[i] == key THEN
        count = count + 1
    ENDIF
NEXT i
PRINT count

Flowchart symbols used and why

SymbolShape to draw on paperWhere it appears hereWhy
TerminatorRounded boxStart and EndOne entry and one shared exit.
Input/outputParallelogramRead array, Print Found, Print Not foundData in and the two possible results out.
ProcessRectangleSet i = 0 and Set i = i + 1They set and update the index.
DecisionDiamondi < n? and a[i] == key?One test keeps the loop inside the list, the other checks for a match.
LoopA decision whose flow line returns to an earlier stepThe arrow from Set i = i + 1 back to i < n?That return arrow makes the search visit every item in turn.

In the interactive diagram every step is a labelled card: the small word above each step names its kind (process, decision or data). When you draw this flowchart by hand, use the standard shapes in the table. The full list is on the flowchart symbols page.

Make your own version

Each link opens the editor with a prompt already filled in. Change the wording, then generate your own flowchart.

How can I use this flowchart?

Download the Linear Search Flowchart as a PNG or SVG, or open it in the studio to edit your own copy. To draw a similar flowchart from a description, use the flowchart generator; for notation and a step-by-step walkthrough, read how to make a flowchart; and browse more flowchart examples for related layouts.

Try the studio with your own description.

Type any system, process, or schema in plain English. ChartCraft will draw it in seconds.