HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Find mirror image of a matrix

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
matrixmirrorimagefind

Problem

Looking for review, good practices, optimizations, clean code tips etc.

/**
 * Flip the columns.
 * 
 * Complexity:
 * O(row * col)
 * 
 */
public final class Mirror {

    private Mirror () { }

    /**
     * Given a matrix create a mirror image,
     * 
     * @param m     the input matrix
     * @throws      NPE if exception occurs.
     */
    public static void mirrorPatch(int[][] m) {
        // for each row.
        for (int i = 0; i < m.length; i++) {
            // for each column
            flipRow(m[i]);
        }
    }

    private static void flipRow(int[] row) {
        int length = row.length;
        // simple swap of each element.
        for (int i = 0; i < length/2; i++) {
            int x = row[i];
            row[i] = row[length -1 - i];
            row[length -1 - i] = x;
        }
    }

    public static void main(String[] args) {
        // even number of columns.
        int[][] m = { {1, 2, 3, 4} , {10, 20, 30, 40}};
        mirrorPatch(m);

        /*
         * Informally verifying that output is:
         * 4   3   2   1  
         * 40  30  20  10
         * 
         */
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[0].length; j++) {
                System.out.print(m[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("-----------------------------------");

        // odd number of columns.

        /*
         * Informally verifying that output is:
         *  5   4   3   2   1
         *  50  40  30  20  10
         */
        int[][] m1 = { {1, 2, 3, 4, 5} , {10, 20, 30, 40, 50}};
        mirrorPatch(m1);

        for (int i = 0; i < m1.length; i++) {
            for (int j = 0; j < m1[0].length; j++) {
                System.out.print(m1[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("-----------------------------------");
    }
}

Solution

In your top=level method mirrorPatch you have:

// for each row.
    for (int i = 0; i < m.length; i++) {
        // for each column
        flipRow(m[i]);
    }


This is unnecessarily verbose, using the iterable nature of arrays you could simply:

for (int[] row : m) {
        flipRow(row);
    }


This is self-documenting, and all good.

In your flipRow method, you do a bit more work, and, it is pretty well structured, and readable. If you want to perhaps squeeze out some more performance, I can suggest some changes. Specifically, since you are creating the length variable, you may as well make it more useful by changing it to be last instead, which simplifies the indexing a little bit. Additionally, sometimes (when it is possible), a subtracting-loop is faster (because the condition is simpler), consider the changes as follows:

private static void flipRow(final int[] row) {
    final int last = row.length - 1;
    for (int i = last/2; i >= 0; i--) {
        int x = row[i];
        row[i] = row[last - i];
        row[last - i] = x;
    }
}

Code Snippets

// for each row.
    for (int i = 0; i < m.length; i++) {
        // for each column
        flipRow(m[i]);
    }
for (int[] row : m) {
        flipRow(row);
    }
private static void flipRow(final int[] row) {
    final int last = row.length - 1;
    for (int i = last/2; i >= 0; i--) {
        int x = row[i];
        row[i] = row[last - i];
        row[last - i] = x;
    }
}

Context

StackExchange Code Review Q#39263, answer score: 2

Revisions (0)

No revisions yet.