Among the following in-place comparison-based sorting algorithms, which…

2024

Among the following in-place comparison-based sorting algorithms, which algorithm generally performs the minimum number of swaps in the worst case?

  1. A.

    Bubble Sort

  2. B.

    Quick Sort

  3. C.

    Selection Sort

  4. D.

    Insertion Sort

  5. E.

    Merge Sort

Attempted by 20 students.

Show answer & explanation

Correct answer: C

Correct answer: Selection Sort.

The stem restricts us to in-place, comparison-based sorts and asks which one performs the minimum number of swaps in the worst case. Among Bubble, Quick, Selection and Insertion Sort, the deciding factor is how many times two elements are actually exchanged, not how many comparisons are made.

Selection Sort works by finding the minimum (or maximum) of the unsorted part and placing it in its final position with exactly one swap per pass. For an array of n elements it makes n - 1 passes, so it performs at most n - 1 swaps. That is O(n) swaps in every case, including the worst case - independent of how the input is arranged.

The other in-place options need far more exchanges in the worst case:

Bubble Sort: up to n(n - 1)/2 swaps - O(n^2) - when the array is in reverse order.

Insertion Sort: up to O(n^2) shifts/swaps for a reverse-sorted array.

Quick Sort: O(n^2) swaps in its worst case (e.g. an already-sorted array with a poor pivot).

Merge Sort is not a standard in-place sort and is excluded by the in-place qualifier, so it is not the intended answer either.

Because its swap count is the lowest (only Cycle Sort uses fewer memory writes), Selection Sort is preferred when writing to memory is the costly operation - making it the correct choice here.

Explore the full course: Ibps So It Prelims