Skip to main content

GATE CSE 2006Algorithms › Sorting AlgorithmsVerified

GATE CSE 2006 PYQ: Which one of the following in place sorting algorithms needs the minimum number of swaps?

Which one of the following in place sorting algorithms needs the minimum number of swaps?

  1. A
    Quick sort
  2. B
    Insertion sort
  3. C
    Selection sort
    Correct answer
  4. D
    Heap sort

Answer

(C) Selection sort

Approach

Compare the worst-case (maximum) number of swap operations each in-place sorting algorithm can require, and pick the one that needs the fewest. Selection sort performs exactly one swap per outer iteration, giving O(n) swaps, which is asymptotically the minimum among the four.

Selection sort does at most one swap per pass (n-1 swaps overall, i.e. O(n)), whereas insertion and quick sort need $O(n^2)$ swaps in the worst case and heap sort needs O(n log n). Hence selection sort needs the minimum number of swaps, so option C is correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.