Selection Sort Swapping Strategy
What is the effect of the swapping strategy used in the above selection sort implementation compared to a classical selection sort?
public class OptimizedSelectionSort {
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int j = i + 1;
while (j < arr.length) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
j++;
}
}
}
}
A
It enhances stability by avoiding unnecessary swaps between equal elements.
B
It reduces overall swap operations by pre-sorting the elements before the main loop.
C
It results in multiple swaps per pass, which can decrease efficiency compared to the single swap in a classical selection sort.
D
It has no impact on the algorithm’s performance and behaves identically to a standard selection sort.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE