Selection Sort Algorithm Stability
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.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | ngvandangthanh | 1 | 1 | - | 100 |
| #2 | Potter16 | 1 | 1 | 0m 22s | 78 |
| #3 | ianrojas473 | 1 | 1 | 0m 43s | 57 |
| #4 | lsj08030922 | 1 | 1 | 0m 52s | 48 |
Items per page:
10
1 – 4 of 4
