Skip to content
Related Articles

Related Articles

Improve Article

GATE | GATE-CS-2006 | Question 14

  • Last Updated : 28 Jun, 2021
Geek Week

Which one of the following in place sorting algorithms needs the minimum number of swaps?
(A) Quick sort
(B) Insertion sort
(C) Selection sort
(D) Heap sort

Answer: (C)

Let’s try to analyse the number of swaps in each of the given sorting algorithms.

Quick sort – Worst Case input for maximum number of swaps will be already sorted array in decreasing order.

Recurrence for Total number of swaps in this case :
T(n) = T(n-1) + O(n) // O(n) swaps will occur in alternate calls to partition algorithm.
= O(n2)

Insertion sort – Worst Case input for maximum number of swaps will be already sorted array in ascending order.When a new element is inserted into an already sorted array of k size, it can lead to k swaps (in case it is the smallest of all) in worst case. For n-1 iterations of insertion sort, total swaps will be O(n2).

Selection sort – There is no Worst case input for selection sort. Since it searches for the index of kth minimum element in kth iteration and then in one swap, it places that element into its orrect position. For n-1 iterations of selection sort, it can have O(n) swaps.

Heap sort – Total number of swaps in Heap sort can be O(nlogn) as after performing Build-heap which may require O(n) swaps, it performs n-1 extract-min operations resulting into O(nlogn) swaps.

See Question 3 of

This solution is contributed by Pranjul Ahuja

Quiz of this Question

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up
Recommended Articles
Page :