ISC Practicals and Theory - Linear Search
Updated 21 Oct 2026
Linear search (sequential search) finds a value in an array by checking the elements one by one from the start until the value is found or the array ends. It works on any array, sorted or not. This program, written in the main method alone, reads an array of 1 to 20 integers and a key, and prints the position of the first occurrence of the key, or a message saying that it is not found.
Definition
In a linear search, the key is compared with a[0], then a[1], then a[2] and so on. The search stops as soon as one element equals the key, or when all $n$ elements have been compared without a match.
- Array:
12 7 25 7 40, key7.a[0] = 12does not match,a[1] = 7matches, so the search stops at index 1. - Array:
10 20 30 40, key35. All four elements are compared and none matches, so the key is not found.
Position convention used here: the program reports the 1-based position (index + 1), so the first element is position 1.
Duplicates: the loop stops at the first match, so only the first occurrence is reported; later equal values are never examined.
Size: the array holds 1 to 20 elements. Any other size is rejected with an error message.
Algorithm
- Read the number of elements
n. If it is not from 1 to 20, print an error message and stop. - Read the
nelements into the arraya, then read the number to search,key. - Set
pos = -1. Forifrom 0 ton - 1, comparea[i]withkey. At the first match, setpos = i + 1(the index is 0-based, the position is 1-based) and leave the loop. - If
posis still -1, print that the key is not found. Otherwise print the position.
Program
The single for loop in main checks the elements one by one and stops at the first match.
import java.util.Scanner;
public class LinearSearch
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // Scanner for input
System.out.print("Enter the number of elements (1 to 20): ");
int n = sc.nextInt(); // number of elements (1 to 20)
if (n < 1 || n > 20) // range check
{
System.out.println("Invalid input. Size must be from 1 to 20.");
}
else
{
int a[] = new int[n]; // array that stores the elements
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) // read each element
a[i] = sc.nextInt();
System.out.print("Enter the number to search: ");
int key = sc.nextInt(); // value to be searched
int pos = -1; // position of the key, -1 if it is not found
for (int i = 0; i < n; i++)
{
if (a[i] == key) // match found
{
pos = i + 1; // index i is 0-based, position is i + 1
break; // the first occurrence is enough
}
}
if (pos == -1)
System.out.println(key + " is not found in the array.");
else
System.out.println(key + " is found at position " + pos);
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
n | int | Number of elements (1 to 20) |
a | int[] | The array that stores the elements |
i | int | Loop counter over the elements |
key | int | The value to be searched |
pos | int | Position of the first occurrence of key, -1 if it is not found |
Sample input and output
Sample 1
Enter the number of elements (1 to 20): 5
Enter the elements:
12 7 25 7 40
Enter the number to search: 7
7 is found at position 2Sample 2
Enter the number of elements (1 to 20): 4
Enter the elements:
10 20 30 40
Enter the number to search: 35
35 is not found in the array.Sample 3
Enter the number of elements (1 to 20): 1
Enter the elements:
9
Enter the number to search: 9
9 is found at position 1Sample 4
Enter the number of elements (1 to 20): 21
Invalid input. Size must be from 1 to 20.Dry run
Input: n = 5, array 12 7 25 7 40, key = 7.
| Step | i | a[i] | a[i] == key | Action |
|---|---|---|---|---|
| 1 | 0 | 12 | false | continue |
| 2 | 1 | 7 | true | pos = i + 1 = 2, leave the loop |
The loop stops at i = 1, so the second 7 at index 3 is never reached. so pos = 2 and the program prints 7 is found at position 2. This matches the real run for the first sample above.
Variations
- All positions "Search a number in an array and print the positions of all its occurrences."
- Count "Search a number in an array and print how many times it occurs."
- Last occurrence "Search a number in an array and print the position of its last occurrence."
- Binary search "Search a number in a sorted array using binary search."