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 significantly increases the time complexity of the sort.
B
It forces the algorithm to use extra memory for temporary variables.
C
It compromises the stability of the sorting algorithm by potentially reordering equal elements.
D
It can lead to an ArrayIndexOutOfBoundsException under certain conditions.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | ngvandangthanh | 1 | 1 | 0m 00s | 100 |
| #2 | ianrojas473 | 1 | 1 | 0m 43s | 57 |
| #3 | lsj08030922 | 1 | 1 | 0m 52s | 48 |
Items per page:
10
1 – 3 of 3
APFIVE