# GATE | GATE-CS-2009 | Question 10

• Last Updated : 28 Jun, 2021

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

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :