in Algorithms edited by
9,995 views
37 votes
37 votes

Which one of the following is the tightest upper bound that represents the number of swaps required to sort $n$ numbers using selection sort?

  1. $O(\log n$)
  2. $O(n$)
  3. $O(n \log n$)
  4. $O(n^{2}$)
in Algorithms edited by
by
10.0k views

4 Comments

this question is asked 3 times in gate 2006 , 2009 , and  2013
9
9
What a tricky question!! Blink and miss !
2
2
An example where we can find n-1 swaps

23,78,45,8,32,46
0
0
@paraskk TBH most of the gate questions are like that only :P
0
0

2 Answers

40 votes
40 votes
Best answer
In selection max you can do is $n$ swaps..selecting the smallest element from all the elements and replacing it correct position so $O(n)$

Correct Answer: $B$
edited by

4 Comments

reshown by
but tightest upper bound means best case and in best case we'll take sorted array then in that case there would be no requirement for swapping .....we'll simply traverse entire array which would take o(n) time ......correct me if I'm wrong :)
0
0
will the maximum number not be n/2.  If we have made n/2 swaps .. that would mean the rest half of the elements would already be in their right positions
0
0
Number of swap  in selection sort is always n-1
0
0
4 votes
4 votes
Best, Average and worst case will take maximum O(n) swaps.
Selection sort time complexity O(n2) in terms of number of comparisons. Each of these scans requires one swap for n-1 elements (the final element is already in place).
Answer:

Related questions