Stability of Overloaded Bubble Sort Methods
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.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE