Question 1 of 5
A bakery records muffin-batch sizes, including a repeated 3, and runs min-based selectionSort.
public static void selectionSort(int[] data)
{
for (int i = 0; i < data.length - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < data.length; j++)
{
if (data[j] < data[minIndex])
{
minIndex = j;
}
}
int temp = data[i];
data[i] = data[minIndex];
data[minIndex] = temp;
}
}
int[] batches = {9, 3, 7, 3, 5};
| Pass | minIndex chosen | Array after the swap |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 |
(a) Complete the table. If two copies of the minimum exist, use the leftmost remaining one. (b) Explain why pass 1 chooses index 1 rather than index 3. (c) State the final array.