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 Solved by 6 students

Selection Sort Algorithm Stability

Mark
< 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 forces the algorithm to use extra memory for temporary variables.

B

It can lead to an ArrayIndexOutOfBoundsException under certain conditions.

C

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

D

It significantly increases the time complexity of the sort.

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

Question Leaderboard

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

AI Tutor

How can I help?

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