Linear Search Program

2025-11-03 linear search simple search array search Python C C# JavaScript PHP interview questions algorithms

Learn how linear search works with a simple step-by-step explanation and code examples in Python, C, C#, JavaScript, and PHP. This program checks each element in an array one-by-one to find a target value—ideal for beginners and interview preparation.

Program Explanation

What is Linear Search?

Linear Search is a simple searching technique where each element in the list or array is checked one by one until the target element is found. It works on both sorted and unsorted data.

Steps to Solve

  1. Take an array and the target element as input.
  2. Start from the first element of the array.
  3. Compare each element with the target value.
  4. If a match is found → return the position.
  5. If the end of the array is reached → the element is not found.

Key Points

  • Linear search works on both sorted and unsorted lists.
  • Time complexity is O(n) because each element is checked one by one.
  • Easy to implement and suitable for small datasets.

Program Code


arr = [10, 20, 30, 40, 50]
target = int(input("Enter number to search: "))

found = False

for i in range(len(arr)):
    if arr[i] == target:
        print("Element found at index", i)
        found = True
        break

if not found:
    print("Element not found")
      

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int target, i, found = 0;

    printf("Enter number to search: ");
    scanf("%d", &target);

    for (i = 0; i < 5; i++) {
        if (arr[i] == target) {
            printf("Element found at index %d", i);
            found = 1;
            break;
        }
    }

    if (!found)
        printf("Element not found");

    return 0;
}
      

let arr = [10, 20, 30, 40, 50];
let target = parseInt(prompt("Enter number to search:"));

let found = false;

for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
        console.log("Element found at index", i);
        found = true;
        break;
    }
}

if (!found)
    console.log("Element not found");
      

using System;

class Program {
    static void Main() {
        int[] arr = {10, 20, 30, 40, 50};

        Console.Write("Enter number to search: ");
        int target = int.Parse(Console.ReadLine());

        bool found = false;

        for (int i = 0; i < arr.Length; i++) {
            if (arr[i] == target) {
                Console.WriteLine("Element found at index " + i);
                found = true;
                break;
            }
        }

        if (!found)
            Console.WriteLine("Element not found");
    }
}
      

<?php
$arr = array(10, 20, 30, 40, 50);
$target = intval(readline("Enter number to search: "));

$found = false;

for ($i = 0; $i < count($arr); $i++) {
    if ($arr[$i] == $target) {
        echo "Element found at index $i";
        $found = true;
        break;
    }
}

if (!$found)
    echo "Element not found";
?>
      
1