Insertion Sort Algorithm Stability
Consider the implementation of the insertion sort algorithm shown above. Which of the following statements correctly describes a key property of this algorithm?
public class InsertionSortStable {
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 only if a special swap method is used.
B
The stability of insertion sort depends on the initial order of the array.
C
Insertion sort is unstable because it involves multiple shifts.
D
Insertion sort is stable because it does not change the relative order of equal elements.
Question Leaderboard
Not enough data yet to show leaderboard.
