| preferred AP College board partner for AP classes
AP Computer Science A/Unit 1: Primitive Types
Start Practice TestPractice Test
About Exam
hard
Selection Sort Algorithm Stability
< Prev
Next >

The selectionSort method shown below implements the selection sort algorithm. Which of the following statements best describes the stability of this algorithm when used to sort an array of objects based on a key field?

public class SelectionSort {
    public static void selectionSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i+1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex])
                    minIndex = j;
            }
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }
}
A

The stability of selection sort depends on the implementation of the swap operation.

B

Selection sort is stable because it preserves the order of equal elements.

C

Selection sort is not stable because it may swap non-adjacent elements, disrupting the original order of equal keys.

D

Selection sort is stable only when sorting primitive int types.

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|Privacy Policy