Insertion Sort Stability
Based on the provided insertionSort method, which statement best explains why this algorithm is considered stable?
public class StableSortExample {
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while(j >= 0 && arr[j] > key) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
}
}
A
Insertion sort is stable because it does not change the relative order of duplicate elements when repositioning the key element.
B
Insertion sort’s stability is due to the use of recursion in its implementation.
C
Insertion sort is unstable due to its reliance on multiple comparisons and swaps.
D
Insertion sort is stable because it always swaps adjacent elements, ensuring order is preserved.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE