MCS-211 · Block 1 Unit 3

Complexity Analysis of Simple Algorithms

Section 6 of 8

Block 1 · Unit 3Complexity Analysis of Simple Algorithms9 min · Section 6 of 8

3.6Linear Search: Best, Worst and Average Case

Covers IGNOU MCS-211 §3.4.5

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 found
key 25, fixed version  : found

Analysis

The basic operation is the comparison a[i] == key.

CaseWhen it happensComparisonsOrder
Best caseKey is the first element1Θ(1)\Theta(1)
Worst caseKey is the last element, or not presentnnΘ(n)\Theta(n)
Average case (key present, each position equally likely)—n+12\dfrac{n+1}{2}Θ(n)\Theta(n)
Unsuccessful search (any input)Key not presentnnΘ(n)\Theta(n)

The average follows from Unit 2’s expectation formula: ∑i=1n1n⋅i=n+12\sum_{i=1}^{n} \frac1n \cdot i = \frac{n+1}{2}. The study material rounds this to “about n/2n/2”; both are Θ(n)\Theta(n). Writing O(n/2)O(n/2) is unusual — inside O, the constant 12\tfrac12 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 nn, average (n+1)/2(n+1)/2 (successful); unsuccessful always nn.
  • 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 1 · multiple choice

How many comparisons does linear search make to find 9 in [7,17,3,9,25,18][7, 17, 3, 9, 25, 18]?

Show answer

Answer: B. 4

9 is the fourth element, so 4 comparisons (7, 17, 3, 9).

Question 2 · multiple choice

What is the average number of comparisons for a successful linear search on 11 elements (all positions equally likely)?

Show answer

Answer: C. 6

(n + 1)/2 = 12/2 = 6. The value 5.5 = n/2 is only an approximation.

Question 3 · multiple choice

What does this program print?

C · quiz_braces.c
#include <stdio.h>

int main(void)
{
    int a[] = {4, 8, 15};
    int count = 0;
    for (int i = 0; i < 3; i++) {
        if (a[i] == 15)
            count = count + 10;
        count = count + 1;
    }
    printf("%d\n", count);
    return 0;
}
Show answer

Answer: D. 13

count + 1 is outside the if, so it runs on all 3 iterations (3), and count + 10 runs once (10): 13. 11 assumes both lines belong to the if.

Question 4 · multiple choice

Which input gives the worst case of linear search?

Show answer

Answer: C. The key is not present.

An absent key (or a key in the last position) forces n comparisons. Sorting does not help linear search.

Question 5 · multiple choice

Why is worst-case analysis usually preferred?

Show answer

Answer: A. It gives a guaranteed upper limit on the running time for every input of size nn.

The worst case guarantees performance and is easy to compute. The average case, not the worst case, needs a probability distribution.

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).

↑ ↓ move↵ openTab switch type