Skip to content
Official PYQ · 1996

GATE CS 1996 Q2.13 — Searching, Sorting, Hashing

Explain with AI

The average number of key comparisons required for a successful search for sequential search on nn items is

Choose one option
View worked solution

Answer: C

For a successful sequential (linear) search on nn items, the target element must be present in the list. Let us assume that the element is equally likely to be at any of the nn positions (uniform probability distribution).

  • If the element is at the 1st1^{\text{st}} position, it requires 11 comparison.
  • If the element is at the 2nd2^{\text{nd}} position, it requires 22 comparisons.
  • …\dots
  • If the element is at the ithi^{\text{th}} position, it requires ii comparisons.
  • If the element is at the nthn^{\text{th}} position, it requires nn comparisons.

Since each position has an equal probability of 1n\dfrac{1}{n}, the average (expected) number of key comparisons is given by:

Average Comparisons=∑i=1n(Probability of position i×Comparisons for position i)\text{Average Comparisons} = \sum_{i=1}^{n} \left( \text{Probability of position } i \times \text{Comparisons for position } i \right)=∑i=1n(1n×i)=1n∑i=1ni= \sum_{i=1}^{n} \left( \frac{1}{n} \times i \right) = \frac{1}{n} \sum_{i=1}^{n} i

Using the sum of the first nn natural numbers, ∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \dfrac{n(n+1)}{2}:

Average Comparisons=1n×n(n+1)2=n+12\text{Average Comparisons} = \frac{1}{n} \times \frac{n(n+1)}{2} = \frac{n+1}{2}

Hence, the correct option is C.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions