Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice TestPractice Test
About Exam
hard Solved by 1 students

Insertion Sort Stability

Mark
< Prev
Next >

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 is stable because it always swaps adjacent elements, ensuring order is preserved.

C

Insertion sort’s stability is due to the use of recursion in its implementation.

D

Insertion sort is unstable due to its reliance on multiple comparisons and swaps.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|