GATE / CS ARCHIVE

GATE CSE 2013 / QUESTION 6

Selection-sort swap count

Algorithms · Searching, Sorting & Selection · Full GATE CSE 2013 paper

GATE 2013, Q6 1 mark · MCQ
Report issue

Searching, Sorting & Selection

Question 6, GATE 2013: Give the tightest listed upper bound on selection-sort swaps for n numbers: A O(log n), B O(n), C O(n log n), D O(n²).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Each selection-sort pass finds the minimum of the unsorted suffix and moves it using at most one swap. There are n−1 passes, and worst-case inputs require a linear number of swaps. Hence O(n), B; the quadratic comparison count does not imply quadratic swaps.

On pass i, selection sort scans the remaining suffix to locate its minimum, then swaps it into position i. It may perform many comparisons during that scan, but at most one swap. There are n−1 such passes, giving at most n−1 swaps. A cyclic displacement input can require a swap on each pass, so the worst-case number grows linearly, not logarithmically. The commonly remembered quadratic selection-sort cost counts comparisons, not swaps.

Original paper, page 2 · Answer key