Click to reopen instructions.

A small pop quiz. Just asking you to do some code reading.

Questions of

Here is the code for a selection sort. (The swap method does what you’d expect: it swaps the elements in the array at the two indices.)

public static void selection(int[] nums) {
  for (int i = 0; i < nums.length - 1; i++) {
    int idxOfNext = i;
    for (int j = i + 1; j < nums.length; j++) {
      if (nums[j] < nums[idxOfNext]) {
        idxOfNext = j;
      }
    }
    swap(nums, i, idxOfNext);
  }
}

After the first iteration of the outer loop, what element is in nums[0]?

The largest number in the array

The smallest number in the array

The original value of nums[0]

Here is the code for an insertion sort. (The swap method is the same as in the first question.)

public static void insertion(int[] nums) {
  for (int i = 0; i < nums.length; i++) {
    int j = i;
    while (j > 0 && nums[j - 1] > nums[j]) {
      swap(nums, j, j - 1);
      j--;
    }
  }
}

After the first iteration of the outer loop, what element is in nums[0]?

The largest number in the array

The smallest number in the array

The original value of nums[0]