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
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
terminator StartOne Start terminator opens the flowchart.
input/output Read array a, n and keyReads the list, its length n and the key to look for.
process Set i = 0i is the index being checked. Lists are counted from 0.
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.
decision a[i] == key?Is the current item the key? Yes ends the search early. No moves on.
process Set i = i + 1Moves to the next index and loops back to i < n?.
input/output Print "Found at index", iReports the position. The loop stops here, so later items are never checked.
input/output Print "Not found"Reached only after all n items failed the match test.
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?.
Pass
i
i < n?
a[i]
a[i] == key?
Next step
1
0
Yes
7
No
Set i = i + 1
2
1
Yes
3
No
Set i = i + 1
3
2
Yes
9
No
Set i = i + 1
4
3
Yes
4
Yes
Print "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
A decision whose flow line returns to an earlier step
The 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.
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.
Flowchart to check prime number: read n, reject anything below 2, then try every divisor i while i * i <= n. With a trace table for n = 29, pseudocode, and tested Python, C and Java code.
Fibonacci series flowchart: keep the last two terms in a and b, print a, then slide the pair forward with c = a + b. With a trace of the first 7 terms (0 1 1 2 3 5 8), pseudocode, and tested Python, C and Java code.
Flowchart for factorial of a number: start fact at 1, multiply it by every i from 1 to n in a loop, then print it. With a trace for n = 5 (answer 120), pseudocode, tested Python, C and Java code, and the recursive version.
factorialflowchartloopsalgorithmsprogramming
Try the studio with your own description.
Type any system, process, or schema in plain English. ChartCraft will draw it in seconds.