GATE | GATE-CS-2009 | Question 10

What is the number of swaps required to sort n elements using selection sort, in the worst case?
(A) \theta(n)
(B) \theta(n log n)
(C) \theta(n^2 )
(D) \theta(n^2 log n)
(A) Theta(n)
(B) Theta(nLogn)
(C) Theta(n*n)
(D) Theta(n*nLogn)

Answer: (A)

Explanation: Here is Selection Sort algorithm for sorting in ascending order.

   1. Find the minimum value in the list
   2. Swap it with the value in the first position
   3. Repeat the steps above for the remainder of the list (starting at
      the second position and advancing each time)

As we can see from the algorithm, selection sort performs swap only after finding the appropriate position of the current picked element. So there are O(n) swaps performed in selection sort.
Because swaps require writing to the array, selection sort is preferable if writing to memory is significantly more expensive than reading. This is generally the case if the items are huge but the keys are small. Another example where writing times are crucial is an array stored in EEPROM or Flash. There is no other algorithm with less data movement.


Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.