Skip to content
Official PYQ · 2013

GATE CS 2013 Q6 — Searching, Sorting, Hashing

Explain with AI

Which one of the following is the tightest upper bound that represents the number of swaps required to sort nn numbers using selection sort?

Choose one option
View worked solution

Answer: B

In Selection Sort, the algorithm divides the array into a sorted and an unsorted region. In each of the n−1n-1 passes, it performs the following steps:

  1. Finds the minimum (or maximum) element in the unsorted subarray.
  2. Swaps this minimum element with the first element of the unsorted subarray.

Since exactly one swap is performed per pass, the total number of swaps required to sort nn numbers is at most n−1n - 1.

Therefore, the tightest upper bound for the number of swaps is O(n)\mathcal{O}(n).

Comparison of Options:

  • Option A (O(log⁡n)\mathcal{O}(\log n)): Too small; we need up to n−1n-1 swaps in the worst case.
  • Option B (O(n)\mathcal{O}(n)): Correct and the tightest upper bound.
  • Option C (O(nlog⁡n)\mathcal{O}(n \log n)) and Option D (O(n2)\mathcal{O}(n^2)): While these are valid upper bounds, they are not the tightest upper bounds for the number of swaps (though O(n2)\mathcal{O}(n^2) is the tightest upper bound for the number of comparisons).

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

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