Answer:
Here is the Java code for the insertion sort algorithm with the required modifications:
```
import java.util.Scanner;
public class InsertionSort {
// Static variables to count comparisons and swaps
static int comparisons = 0;
static int swaps = 0;
public static void main(String[] args) {
// Step 1: Read the size and elements of the array
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
int[] nums = new int[size];
for (int i = 0; i < size; i++) {
nums[i] = scanner.nextInt();
}
scanner.close();
// Step 2: Output the initial array
printNums(nums);
// Step 3: Perform insertion sort with modifications
insertionSort(nums);
// Step 4: Output the sorted array and counts
printNums(nums);
System.out.println("comparisons: " + comparisons);
System.out.println("swaps: " + swaps);
}
public static void insertionSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
int j = i;
while (j > 0 && nums[j] < nums[j-1]) {
// Swap nums[j] and nums[j-1] and count swaps
swap(nums, j, j-1);
swaps++;
// Increment j and count comparisons
j--;
comparisons++;
}
// Output the array during each iteration of the outside loop
printNums(nums);
}
}
public static int[] readNums() {
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
int[] nums = new int[size];
for (int i = 0; i < size; i++) {
nums[i] = scanner.nextInt();
}
scanner.close();
return nums;
}
public static void printNums(int[] nums) {
System.out.print(nums[0]);
for (int i = 1; i < nums.length; i++) {
System.out.print(" " + nums[i]);
}
System.out.println();
}
public static void swap(int[] nums, int j, int k) {
int temp = nums[j];
nums[j] = nums[k];
nums[k] = temp;
}
}
```
When the input is "6 3 2 1 5 9 8", this program outputs the desired result:
```
6 3 2 1 5 9 8
3 6 2 1 5 9 8
2 3 6 1 5 9 8
1 2 3 6 5 9 8
1 2 3 5 6 9 8
1 2 3 5 6 8 9
comparisons: 7
swaps: 4
```