Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Data Collections
Start Practice TestPractice Test
About Exam
hard

Insertion Sort Algorithm Stability

Mark
< Prev
Next >

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.

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: apfive@apfive.org|Instagram: @_apfive_|Privacy Policy|