Stability of Overloaded Bubble Sort Methods
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.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | chunxiangxu.cxu | 2 | 3 | 0m 27s | 163 |
| #2 | suhanakochhar006 | 1 | 1 | 1m 02s | 38 |
| #3 | zgj07310417 | 0 | 1 | 0m 44s | -54 |
| #4 | y.seong2027 | 1 | 1 | 9m 54s | -494 |
Items per page:
10
1 – 4 of 4
APFIVE