Linear Search - Complete Guide

Learn, Visualize, and Understand Linear Search Algorithm

What is Linear Search?

Linear Search (also called Sequential Search) is the simplest searching algorithm. It checks each element in the array sequentially from the beginning until it finds the target value or reaches the end of the array.

Key Characteristics

  • Simplicity: Easiest searching algorithm to understand and implement
  • Works on unsorted data: Unlike binary search, no sorting required
  • Sequential checking: Examines each element one by one
  • Time Complexity: O(n) - Checks up to n elements in worst case

Real-Life Example

Finding a book on a shelf: Imagine you're looking for a specific book on an unsorted bookshelf.

  1. Start from the leftmost book
  2. Check if it's the book you're looking for
  3. If not, move to the next book
  4. Repeat until you find the book or reach the end

This is exactly how linear search works!

How Linear Search Works

Step 1: Start from the first element (index 0)

Step 2: Compare the current element with the target value

Step 3: Decision:

  • If arr[i] == target → Found! Return index
  • If arr[i] != target → Move to next element (i++)

Step 4: Repeat until element is found or end of array is reached

Complexity Analysis

Case Time Complexity Description
Best Case O(1) Element found at first position
Average Case O(n) Element found in middle
Worst Case O(n) Element at last position or not present
Space Complexity O(1) Only uses constant extra space

Advantages

  • ✅ Simple and easy to understand
  • ✅ Works on unsorted arrays
  • ✅ No preprocessing (sorting) required
  • ✅ Efficient for small datasets
  • ✅ Can be used on any data structure (arrays, linked lists)

Disadvantages

  • ❌ Slow for large datasets (O(n) complexity)
  • ❌ Inefficient compared to binary search for sorted data
  • ❌ Checks every element in worst case

When to Use Linear Search

  • Small datasets (less than 100 elements)
  • Unsorted arrays where sorting would be expensive
  • When you need to search only once
  • Linked lists (where binary search isn't possible)

Interactive Linear Search Visualization

Watch how linear search checks each element sequentially until it finds the target

Unchecked
Checking Now
Found
Not Match

Linear Search - C Code Implementation

See the C code and watch how it executes step-by-step

C Source Code

1#include <stdio.h>
2
3int linearSearch(int arr[], int n, int target) {
4 for (int i = 0; i < n; i++) {
5 if (arr[i] == target) {
6 return i;
7 }
8 }
9 return -1;
10}

Variables

arr[] -
n -
target -
i -
arr[i] -

Console Output

Ready to execute. Click "Execute Code" to start.

Complete C Code with Comments

#include <stdio.h>

// Linear Search Function - Returns index of target element
int linearSearch(int arr[], int n, int target) {
    // Iterate through each element
    for (int i = 0; i < n; i++) {
        printf("Checking index %d: arr[%d] = %d\\n", i, i, arr[i]);
        
        // Check if current element matches target
        if (arr[i] == target) {
            printf("Match found!\\n");
            return i;  // Return index where element is found
        }
        
        printf("Not a match. Continue...\\n");
    }
    
    // Element not found
    return -1;
}

int main() {
    int arr[] = {15, 8, 23, 4, 42, 16, 30, 25};
    int n = sizeof(arr) / sizeof(arr[0]);
    int target = 42;
    
    printf("Array: ");
    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\\nSearching for: %d\\n\\n", target);
    
    int result = linearSearch(arr, n, target);
    
    if (result != -1) {
        printf("\\n✓ Element %d found at index %d\\n", target, result);
    } else {
        printf("\\n✗ Element %d not found\\n", target);
    }
    
    return 0;
}