in Algorithms edited by
27,567 views
30 votes
30 votes

What is the number of swaps required to sort $n$ elements using selection sort, in the worst case?

  1. $\Theta(n)$

  2. $\Theta(n \log  n)$

  3. $\Theta(n^2)$

  4. $\Theta(n^2 \log  n)$

in Algorithms edited by
27.6k views

1 comment

Selection sort:

 

2
2

2 Answers

31 votes
31 votes
Best answer

The answer is A.

In worst case, we have $1$ swap in each loop except the last one and hence $n-1$ swaps at max for $1$ to $n$. Therefore the worst case number of swaps is $\Theta(n)$

edited by

3 Comments

Best case: $0$ swaps. for eg $1,2,3,4,5,6$

Worst case: $n-1$ swaps for eg $6,5,2,1,4,3$
18
18

@reena ma'am, it depends on implementation, but in its basic form, even in best case selection sort requires 1 swap in each pass. https://stackoverflow.com/questions/26688765/what-are-the-number-of-swaps-required-in-selection-sort-for-each-case

1
1
A minor correction, in worst case , selection sort does n-1 swaps.
0
0
4 votes
4 votes

option a

1 comment

Nice I confused in n2 and n. Now my confusion is clear thanks.
0
0
Answer:

Related questions