How many swaps are needed to sort the array {3, 1, 5, 2, 4} using Selection…
2023
How many swaps are needed to sort the array {3, 1, 5, 2, 4} using Selection Sort ?
- A.
2
- B.
3
- C.
4
- D.
5
Attempted by 142 students.
Show answer & explanation
Correct answer: C
The correct answer is 4 swaps.
Here is the step-by-step breakdown of how the Selection Sort algorithm sorts the array {3, 1, 5, 2, 4}:
Initial Array: [3, 1, 5, 2, 4]
Pass 1:
Find the minimum element in the entire array
[3, 1, 5, 2, 4]. The minimum is 1.Swap it with the first element (3).
Array becomes:
[1, 3, 5, 2, 4]Total Swaps: 1
Pass 2:
Find the minimum element in the remaining unsorted part
[3, 5, 2, 4]. The minimum is 2.Swap it with the first unsorted element (3).
Array becomes:
[1, 2, 5, 3, 4]Total Swaps: 2
Pass 3:
Find the minimum element in the remaining unsorted part
[5, 3, 4]. The minimum is 3.Swap it with the first unsorted element (5).
Array becomes:
[1, 2, 3, 5, 4]Total Swaps: 3
Pass 4:
Find the minimum element in the remaining unsorted part
[5, 4]. The minimum is 4.Swap it with the first unsorted element (5).
Array becomes:
[1, 2, 3, 4, 5]Total Swaps: 4
After 4 passes, the last element (5) is automatically in its correct position. The array is completely sorted with exactly 4 swaps.