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

Consider the following selectionSort method, which is intended to sort an array of integers in ascending order.

public class SelectionSortMistake {
    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[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }
}

What is the primary consequence of using the <= operator instead of < in the inner loop’s conditional statement?

A

It significantly increases the time complexity of the sort.

B

It forces the algorithm to use extra memory for temporary variables.

C

It compromises the stability of the sorting algorithm by potentially reordering equal elements.

D

It can lead to an ArrayIndexOutOfBoundsException under certain conditions.

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1ngvandangthanh11 0m 00s 100
#2ianrojas47311 0m 43s 57
#3lsj0803092211 0m 52s 48
Items per page:
10
1 – 3 of 3
No comments yet. Be the first to comment!

AI Tutor

How can I help?

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