GATE / CS ARCHIVE

GATE CSE 2009 / QUESTION 11

Selection-sort worst-case swaps

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

GATE 2009, Q11 1 mark · MCQ
Report issue

Searching, Sorting & Selection

Question 11, GATE 2009: What is the worst-case number of swaps in selection sort on n elements? A Θ(n); B Θ(n log n); C Θ(n²); D Θ(n² log n).
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Each pass places one selected minimum using at most one swap, and there are n−1 passes. Inputs exist requiring a linear number of those swaps, so the worst-case swap count is Θ(n), A, despite Θ(n²) comparisons.

Selection sort scans the remaining suffix to find its minimum and moves that minimum into the next position. There are n−1 such placements and at most one swap per placement. Some inputs require a swap in a linear number of passes, giving a tight Θ(n) worst-case swap count. This differs from its Θ(n²) comparison count, since each pass may examine many elements before performing only one exchange.

Original paper, page 2