Linear search was introduced in Unit 1; here it is analysed in the style of this unit, and the study material’s code is checked. Linear search is the simplest searching method and works on any list, sorted or not.
The algorithm
Compare the key with each element in turn. Stop as soon as it matches; if the end is reached, the key is not present.
C · search_braces.c
#include <stdio.h>#include <stdbool.h>/* The loop body as printed, without braces around the if-part. */bool search_printed(const int a[], int n, int key){ bool found = false; int i; for (i = 0; i < n; i++) { if (a[i] == key) found = true; break; /* runs on the FIRST iteration, always */ } return found;}/* Corrected: break only when the key is found. */bool search_fixed(const int a[], int n, int key){ for (int i = 0; i < n; i++) { if (a[i] == key) return true; } return false;}int main(void){ int a[] = {7, 17, 3, 9, 25, 18}; int n = 6; printf("key 25, printed version: %s\n", search_printed(a, n, 25) ? "found" : "not found"); printf("key 25, fixed version : %s\n", search_fixed(a, n, 25) ? "found" : "not found"); return 0;}
Output
key 25, printed version: not foundkey 25, fixed version : found
Analysis
The basic operation is the comparison a[i] == key.
Case
When it happens
Comparisons
Order
Best case
Key is the first element
1
Θ(1)
Worst case
Key is the last element, or not present
n
Θ(n)
Average case (key present, each position equally likely)
—
2n+1
Θ(n)
Unsuccessful search (any input)
Key not present
n
Θ(n)
The average follows from Unit 2’s expectation formula: ∑i=1nn1⋅i=2n+1. The study material rounds this to “about n/2”; both are Θ(n). Writing O(n/2) is unusual — inside O, the constant 21 is dropped.
Why the worst case is the default
It is a guarantee: the algorithm never takes longer. Real-time and safety-critical systems (flight control, medical devices) need such guarantees.
It is usually easier to compute than the average case, which needs a probability distribution of inputs.
The best case says little: almost any algorithm is fast on its luckiest input.
The average case is still valuable when the input distribution is known, and it explains why algorithms such as quicksort are fast in practice.
Key takeaways
The basic operation of searching is the key comparison.
Best 1, worst n, average (n+1)/2 (successful); unsuccessful always n.
Worst case is the default because it is a guarantee and easy to compute.
In C, an if without braces controls only one statement.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a correct C function int search(const int a[], int n, int key) that returns the index of key or −1, and state its best- and worst-case number of comparisons.
Show worked answer
int search(const int a[], int n, int key) { for (int i = 0; i < n; i++) { if (a[i] == key) return i; } return -1; } Best case: key at index 0, 1 comparison, Θ(1). Worst case: key last or absent, n comparisons, Θ(n).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-211/complexity-analysis-of-simple-algorithms/linear-search-best-worst-and-average-case/ to read this page.