Selection Sort Algorithm Stability
The selectionSort method shown below implements the selection sort algorithm. Which of the following statements best describes the stability of this algorithm when used to sort an array of objects based on a key field?
public class SelectionSort {
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i+1; j < arr.length; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
A
The stability of selection sort depends on the implementation of the swap operation.
B
Selection sort is stable because it preserves the order of equal elements.
C
Selection sort is not stable because it may swap non-adjacent elements, disrupting the original order of equal keys.
D
Selection sort is stable only when sorting primitive int types.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE