Question
Which one of the following is the tightest upper bound that represents the number of swaps required to sort numbers using selection sort?
Official answer and solution
- Official answer
- B
- From
- Official GATE 2013 answer key
Solution
Analyze the number of element swap operations executed during selection sort on an array of size n.
In standard selection sort:
- The algorithm makes passes across the array.
- In the -th pass, it searches the remaining unsorted subarray for the minimum element and performs at most 1 swap to place that element into index .
- Across all passes, the maximum total number of swaps is at most .
Therefore, the number of swaps is bounded by .
(Note that while the number of comparisons is , the number of swaps is strictly ).
Option (B)
Confusing the number of comparisons () with the number of swaps ().
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2013 Computer Science and Information Technology (CS)
- Question
- 6 of 65, printed as CS Q.6
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.