Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 42

Linear search

CSorting and searchingJNTUH R22 · CS108ES

Aim

To write a C program that uses a non-recursive function to search for a key value in a list of integers using linear search, and prints its position or a "not found" message.

Algorithm

  1. Read n (1 to 100) and then n integers into an array.
  2. Read the key to search for.
  3. In the function, compare the key with each element, from index 0 to n − 1.
  4. If an element equals the key, return its index.
  5. If the loop ends without a match, return −1.
  6. If the result is −1, print that the key was not found. Otherwise print its position.

Program

C
#include <stdio.h>

#define MAX 100

/* Returns the index of key in a[0..n-1], or -1 if key is not present */
int linear_search(const int a[], int n, int key) {
    for (int i = 0; i < n; i++) {
        if (a[i] == key) {
            return i;
        }
    }
    return -1;
}

int main(void) {
    int a[MAX], n, key;

    printf("Enter the number of elements (1 to %d): ", MAX);
    if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
        printf("Invalid number of elements\n");
        return 1;
    }
    printf("Enter %d integers: ", n);
    for (int i = 0; i < n; i++) {
        if (scanf("%d", &a[i]) != 1) {
            printf("Invalid input\n");
            return 1;
        }
    }
    printf("Enter the key to search: ");
    if (scanf("%d", &key) != 1) {
        printf("Invalid input\n");
        return 1;
    }

    int index = linear_search(a, n, key);
    if (index == -1) {
        printf("%d is not found in the list\n", key);
    } else {
        printf("%d is found at position %d (index %d)\n", key, index + 1, index);
    }
    return 0;
}

Output

Key is in the list

Output 1
Enter the number of elements (1 to 100): 6
Enter 6 integers: 34 12 56 7 89 23
Enter the key to search: 7
7 is found at position 4 (index 3)

Key is not in the list

Output 2
Enter the number of elements (1 to 100): 6
Enter 6 integers: 34 12 56 7 89 23
Enter the key to search: 50
50 is not found in the list

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

The output gives both the position (counting from 1) and the array index (counting from 0). In the first run, 7 is the 4th number, so it is at position 4 and index 3.

Linear search works on any list, sorted or not. In the worst case (the key is last or missing) it compares the key with all n elements, so it takes O(n) time.

Common mistakes

  • Printing "not found" inside the loop, in the else part of the if. The program then prints "not found" for every element that does not match. Decide "not found" only after the whole loop has finished.

  • Writing the loop as i <= n. The last valid index is n - 1, so a[n] is outside the array.

  • Returning 0 for not found. 0 is a valid index (the first element), so use −1 instead.

  • Not checking that n is within the array size. Entering 500 for an array of 100 writes past the end of the array.

Viva questions

Try to answer each one aloud before you open it.

  • What is linear search?

    It checks the elements one by one from the start until it finds the key or reaches the end of the list.

  • What is the time complexity of linear search?

    Best case O(1), when the key is the first element. Worst case and average case O(n).

  • Does linear search need the list to be sorted?

    No. It works on any list. Binary search is the one that needs a sorted list.

  • What does non-recursive mean here?

    The function uses a loop to go through the list and does not call itself.

  • If the key appears more than once, which position does this program report?

    The first one, because the function returns as soon as it finds a match.

Learn the concepts

All Programming for Problem Solving Lab experiments →