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

Consider the BubbleSorter class, which contains two overloaded sort methods. Which statement correctly describes the stability of these sorting algorithms?

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

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

B

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

C

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

D

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

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
#1chunxiangxu.cxu23 0m 27s 163
#2suhanakochhar00611 1m 02s 38
#3zgj0731041701 0m 44s -54
#4y.seong202711 9m 54s -494
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: [email protected]|Privacy Policy