In Unit 8 Assessment Question 4, the explanation about the answer seems to be incorrect. This part is wrong: " temp
(8
) is greater than the value at index - 1
(11
)", as a result the subsequent steps are incorrect. However, the answer seems to be correct.
Below is what I came up with - please correct me if I got it wrong:
The array after the first iteration in the for loop is unchanged.
In the second for-loop iteration:
The array after the first pass in while-loop is [ 11, 8, 8, 7, 21, 15, 1]
The array after the second pass in the while-loop is [11, 11, 8, 7, 21, 15, 1]
After existing the while loop, nums.set(index, temp) sets the first element to 12, hence the array becomes [12, 11, 8, 7, 21, 15, 1]. This is the result of executing the for loop twice.