Linear search in C: solved exercise on unsorted arrays

Linear search in C: solved exercise step by step

If you searched for a solved linear search exercise in C, here is the full implementation with index return and the element-not-found case.

Linear search (also called sequential search) scans the array element by element until it finds the target value or reaches the end. It is the simplest search algorithm and the only one that works on unsorted arrays.

Problem statement

  1. Implement linear_search that receives an array, its size, and a target value, and returns the index of the first occurrence or -1 if not found.
  2. Test it with a successful search and a value that is not in the array.
  3. Also implement linear_search_all that returns every index where the value appears (for repeated elements).

C solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>

/* Returns the index of the first occurrence of target, or -1 if not found */
int linear_search(const int a[], int n, int target) {
    for (int i = 0; i < n; i++) {
        if (a[i] == target) {
            return i;
        }
    }
    return -1;
}

/* Prints every index where target appears */
void linear_search_all(const int a[], int n, int target) {
    int found = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] == target) {
            printf("  Found at index %d\n", i);
            found = 1;
        }
    }
    if (!found) {
        printf("  Not found\n");
    }
}

int main(void) {
    int data[] = { 7, 3, 9, 3, 1, 5, 3, 8 };
    int n = (int)(sizeof(data) / sizeof(data[0]));

    /* Successful search */
    int idx = linear_search(data, n, 5);
    printf("Search 5: index %d\n", idx);

    /* Failed search */
    idx = linear_search(data, n, 42);
    printf("Search 42: index %d (not found)\n", idx);

    /* All occurrences of 3 */
    printf("All positions of 3:\n");
    linear_search_all(data, n, 3);

    return 0;
}

Expected output

1
2
3
4
5
6
Search 5: index 5
Search 42: index -1 (not found)
All positions of 3:
  Found at index 1
  Found at index 3
  Found at index 6

Complexity

CaseComparisonsComplexity
Best case (first element)1O(1)
Average casen/2O(n)
Worst case (last or not found)nO(n)
FeatureLinearBinary
Requires sorted arrayNoYes
ComplexityO(n)O(log n)
ImplementationVery simpleModerate
Best forSmall or unsorted arraysLarge already-sorted arrays

Common mistakes

  • Returning 0 instead of -1 when not found: 0 is the valid index of the first element.
  • Using >= instead of < in the loop condition and going out of bounds.
  • Continuing the loop after finding the element when only the first occurrence is needed.

Practical use

Linear search is used when:

  • the array is unsorted and sorting it is not worth the cost,
  • the array is small (< ~100 elements) and performance difference is irrelevant,
  • you need to find all occurrences of a value, not just the first.

Guided practice and next step

If you want a complete path with progressive difficulty:

FAQ

When the array is unsorted. Sorting it first would cost O(n log n), which only pays off if you need to run many searches afterward.

Can linear search work on string arrays?

Yes, but you must use strcmp instead of ==: if (strcmp(a[i], target) == 0).

Is there a standard linear search function in C?

Not directly. bsearch from <stdlib.h> performs binary search (requires a sorted array). For linear search you need to implement it manually.