| preferred AP College board partner for AP classes
AP Computer Science A/Unit 5: Writing Classes
Start Practice TestPractice Test
Share
About Exam
medium Solved by 4 students
Stability of Overloaded Bubble Sort Methods
< Prev
Next >

Consider the two overloaded bubble sort methods in the BubbleSorter class. Which statement about their stability is correct?

public class BubbleSorter {
    public void sort(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr.length - 1; j++) {
                if(arr[j] > arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    
    public void sort(double[] arr) {
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr.length - 1; j++) {
                if(arr[j] > arr[j+1]) {
                    double temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
}
A

Both methods may become unstable when sorting objects, but for primitive types, stability is not a concern.

B

The overloaded methods create ambiguity during sorting, making it unclear which method maintains order correctly.

C

Only the integer sort method is stable, while the double sort method is not due to floating-point precision issues.

D

Both methods are stable because they only swap elements when necessary, preserving the order of equal elements.

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

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

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