MCS-208 · Unit 9

Searching and Sorting Techniques

Section 1 of 9

Block 3 · Unit 9Searching and Sorting Techniques8 min · Section 1 of 9

9.1Searching Basics and Linear Search

Covers IGNOU MCS-208 §9.0, 9.1, 9.2

To search is to find the record whose key matches a given value. The simplest method looks at every record in turn. This page defines the vocabulary and analyses that method.

Files, records and keys

A file (or table) is a collection of records, and each record is a collection of fields. A field used to identify records is a key. In a telephone directory, each record has the fields name and phone number; usually the name is the key, but a reverse directory would use the number as the key. A search takes an argument aa and finds the record whose key equals aa (a successful search) or reports that there is none (unsuccessful).

It works on any array, sorted or not, and on linked lists.

Efficiency

CaseComparisons
Best (key at the first position)1
Worst (key at the last position, or absent)nn
Average, successful (each position equally likely)1+2+⋯+nn=n+12\dfrac{1 + 2 + \dots + n}{n} = \dfrac{n + 1}{2}
Unsuccessfulnn

So linear search is O(n)O(n). For small or unsorted arrays, and for a single search, it is perfectly reasonable.

Key takeaways

  • File → records → fields; the key identifies a record.
  • Linear search: compare one by one, stop at the first match; works on unsorted data.
  • Best 1, average (n+1)/2(n+1)/2 (successful), worst and unsuccessful nn: O(n)O(n).
  • A sentinel saves one test per step.

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 a successful linear search need on average in an array of 99 elements, if each position is equally likely?

Show answer

Answer: C. 50

(n+1)/2=100/2=50(n + 1)/2 = 100/2 = 50. 7 is about the number of probes binary search would need on sorted data.

Question 2 · multiple choice

Where must the key be for the best case of linear search?

Show answer

Answer: A. At the first position

Then one comparison is enough. The last position (or absence) is the worst case.

Question 3 · multiple choice

Does sorting the array improve the order of linear search?

Show answer

Answer: C. No, it stays O(n)O(n).

Linear search still examines elements one by one. (An unsuccessful search can stop early once a larger key is seen, but the worst case is still nn.) Only a different method, binary search, uses the order.

Question 4 · multiple choice

What does this program print?

C · q9-linear.c
#include <stdio.h>

int main(void)
{
    int m[] = {8, 3, 9, 3, 7};
    int count = 0, pos = -1;
    for (int k = 0; k < 5; k++) {
        count++;
        if (m[k] == 3) { pos = k; break; }
    }
    printf("pos=%d comparisons=%d\n", pos, count);
    return 0;
}
Show answer

Answer: B. pos=1 comparisons=2

The first 3 is at index 1, found on the second comparison, and break stops the loop. Without break, the loop would continue and end with pos=3 after 5 comparisons.

Question 5 · multiple choice

What is the purpose of a sentinel in linear search?

Show answer

Answer: D. To guarantee the loop stops, so the bounds test k < n can be dropped

The key placed after the last element is always found, so only one comparison per step is needed. The order is still O(n)O(n).

Question 6 · short answer

Write a C function for linear search that returns the position of the key or −1, and give its best, average and worst number of comparisons.

Show worked answer

int linear_search(const int m[], int n, int el) { for (int k = 0; k < n; k++) if (m[k] == el) return k; /* stop at the first match */ return -1; /* unsuccessful */ } Best case: 1 comparison (key at index 0). Worst case: nn (key last or absent). Average for a successful search with every position equally likely: (1+2+⋯+n)/n=(n+1)/2(1 + 2 + \dots + n)/n = (n + 1)/2. Order O(n)O(n).

↑ ↓ move↵ openTab switch type