| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice TestPractice Test
About Exam
hard
Insertion Sort Stability
< 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’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.

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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy