j.doe j.doe - 10 days ago 6
Java Question

Printing where the last inserted array position

I would like to print an array where it states where the "placedPosition", or the parameter's location in the array. This position is the location that the latest number was inserted into the sorted part of the array.It is the same for the "currPosition", but I can figure that out if I get help on the placed. "i" is the placedPosition, and "c" is the "currPosition". It should look something like this:

enter image description here

I'm really not sure how to go about this, here is the code I have thus far:

public static void displaySort(int[] items, int currPosition, int placedPosition) {
System.out.println("Iteration #"+currPosition);
String[][] arr = new String[currPosition][currPosition];
String i="i";
String c="c";
String ic="ic";
System.out.println(arr[placedPosition][placedPosition]=i);
System.out.println(arr[currPosition][currPosition]=c);
System.out.println(Arrays.toString(items));
}


Sorting:

public static void insertionSort(int[] items) {
int currentPosition; // the number of items sorted so far
int currentItem; // the value being inserted
int backwardMovingIndex; // used to find where the current value gets inserted.

//displaySort(items, 0, 0); // use this call to display the list before the first iteration (iteration 0)

for (currentPosition = 1; currentPosition < items.length; currentPosition++) // Start with 1 (not 0)
{
currentItem = items[currentPosition];
for (backwardMovingIndex = currentPosition - 1; (backwardMovingIndex >= 0) && (items[backwardMovingIndex] > currentItem); backwardMovingIndex--) // Larger values are moving up
{
items[backwardMovingIndex + 1] = items[backwardMovingIndex];
}
items[backwardMovingIndex + 1] = currentItem; // Put the current value in its proper location
displaySort(items, currentPosition, backwardMovingIndex + 1); // use this call to display the list for all other iterations
}
}

Answer

For starters, you need to make sure the numbers are printed out with a fixed number of digits, so instead of System.out.println(Arrays.toString(items)); try using code like

    for (int item : items) {
        System.out.printf("%5d", item);
    }
    System.out.println();

To place a an "i" in column i use code like

    int i = 1;
    for (int x = 0; x<items.length; x++) {
        if (x == i) {
            System.out.printf("%5s", "i");
        }
        else {
            System.out.printf("%5s", "");
        }
    }
    System.out.println();

The key is using the same value (in this example, 5) for the format specifier controlling the column width in all branches of the loop. If these are arbitrary integers, you might need to use a larger value, such as 10. If you want to get fancy, you can figure out how wide you need to make the columns by looking at the base 10 logrithm of the largest absoulte value in the array. You can then convert an integer into a string of exactly n characters with something like String.format("%"+n+"d", item)

To complete your problem add more branches to the if/else structure to check for (x == c) and (i == c) && (x == i)