• edited by
18,238 views
55 55 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}$)

3 Answers

Best answer
53 53 votes
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
7 7 votes
Correct Answer : $B$

Best, Average and worst case will take maximum $O(n)$ swaps.
Selection sort time complexity $O(n^2)$ in terms of number of comparisons. Each of these scans requires one swap for $n-1$ elements (the final element is already in place).
• edited by
1 1 vote
Sure, let's go through an example to demonstrate the number of swaps in selection sort.

Consider the array \( A = [64, 25, 12, 22, 11] \).

Step-by-Step Selection Sort with Swaps:

1. Initial array:\([64, 25, 12, 22, 11]\)

   - Find the minimum element (11).

   - Swap it with the first element (64).

   - Array after 1st swap:** \([11, 25, 12, 22, 64]\)

2.Second iteration \([11, 25, 12, 22, 64]\)

   - Find the minimum element from the remaining unsorted portion (12).

   - Swap it with the second element (25).

   - Array after 2nd swap: \([11, 12, 25, 22, 64]\)

3. Third iteration:\([11, 12, 25, 22, 64]\)

   - Find the minimum element from the remaining unsorted portion (22).

   - Swap it with the third element (25).

   - Array after 3rd swap:\([11, 12, 22, 25, 64]\)

4. Fourth iteration:\([11, 12, 22, 25, 64]\)

   - The minimum element from the remaining unsorted portion is already in its place (25).

   - No swap needed, but if we were to count this as a swap, it would be with itself.

After sorting, the array is \([11, 12, 22, 25, 64]\). The total number of swaps is 3.

General Case:

In a general case, for an array of \( n \) elements, the selection sort makes exactly \( n - 1 \) swaps. This is because each iteration, except the last one, performs one swap to place the minimum element in its correct position.

So, for \( n \) elements, the mathematical expression for the number of swaps is:

\[ S(n) = n - 1 \]

where \( S(n) \) is the number of swaps required to sort the array.
Answer:
Position:
Show:

Related questions

43 43 votes
2 answers 2 answers
35.6k
35.6k views
Kathleen asked Sep 22, 2014
35,597 views
What is the number of swaps required to sort $n$ elements using selection sort, in the worst case?$\Theta(n)$$\Theta(n \log n)$$\Theta(n^2)$$\Theta(n^2 \log n)$
8 8 votes
6 6 answers
3.9k
3.9k views
Arjun asked Feb 27, 2025
3,869 views
Suppose that insertion sort is applied to the array $[1,3,5,7,9,11, x, 15,13]$ and it takes exactly two swaps to sort the array. Select all possible values of $x$.$10$$12...
9 9 votes
3 answers 3 answers
12.8k
12.8k views
ajit asked Sep 20, 2015
12,770 views
How many comparisons are needed to sort an array of length $5$ if a straight selection sort is used and array is already in the opposite order?$1$$10$$15$$20$
3 3 votes
2 2 answers
162
162 views
GO Classes asked Aug 12
162 views
What effect does the initial ordering of the records have on the number of comparisons performed by standard Selection Sort?No effect Only a constant-factor difference Th...