Question 1 of 5
A lantern crew stores hang times in an ArrayList and runs min-based selectionSort.
public static void selectionSort(ArrayList<Integer> data)
{
for (int i = 0; i < data.size() - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < data.size(); j++)
{
if (data.get(j) < data.get(minIndex))
{
minIndex = j;
}
}
int temp = data.get(i);
data.set(i, data.get(minIndex));
data.set(minIndex, temp);
}
}
ArrayList<Integer> hang = new ArrayList<Integer>();
hang.add(5);
hang.add(1);
hang.add(8);
hang.add(3);
| Pass | List after the pass | Value swapped into the next left slot |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 |
(a) Complete the table for all three passes. (b) Identify the two indexes swapped on pass 2. (c) Explain why the value placed on the left during pass 1 will not move again.