GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Count selection-sort exchanges per pass

3 questions solved the same way, newest first. Back to Algorithms

How these are solved

Use at most one exchange after each selection pass, independent of the quadratic number of comparisons. 2006 identifies the sorting algorithm with fewest swaps;

1 mark · MCQ
Report issue

Asked again GATE 2009, Q11 (same question)

Selection-sort swap count

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

1 mark · MCQ
Report issue

Asked again GATE 2013, Q6 (same question)

Selection-sort worst-case swaps

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

1 mark · MCQ
Report issue

Sorting with few swaps

Searching, Sorting & Selection

Question 14, GATE 2006: Among the standard in-place sorting algorithms listed, which needs the fewest swaps under the conventional worst-case exchange-count comparison?
Options:
(A) Quicksort
(B) Insertion sort
(C) Selection sort
(D) Heapsort
Tap to enlarge
Your answer
Answer & solution

Answer: C under the standard swap-based comparison (no official key was published)

1. Selection sort places the minimum remaining item in its final location with at most one exchange per pass, hence at most n−1 swaps. Standard swap-based insertion sort, quicksort and heapsort can use more.

2. Thus selection sort, C, in the conventional worst-case exchange-count comparison. A shift-based insertion implementation can avoid an explicit swap operation, so literal source-code swap counts are implementation-dependent.

3. Teaching check: Selection sort can inspect the entire unsorted suffix without moving any item, then exchange its minimum with the first unsorted position. This separates comparison count, Θ(n²), from exchange count, O(n). The question asks about swaps, not fastest running time.

Original paper, page 2