eagertoLearn
eagertoLearn

Reputation: 10132

method to do in place reversal of columns in a 2D array in Java

The problem is given a 2D array write a method to reversal of columns. If possible, do it in-place I have implemented and it works fine.but it is not in-place.It uses auxillary storage, is it possible to reverse columns of a 2D array without using matrix.

Here is my code:
public static int[][] reverseColumns(int[][] matrix){
        int rows=matrix.length;
        int cols=matrix[0].length;
        int temp=0;

        int[][] result= new int[rows][cols];

        for (int row=rows-1; row >=0; row--){
            for (int col=0;col<cols;col++){
                result[row][col]=matrix[temp][col];
            }
            temp++;
        }
        return result;
    }

  public static void print2DArray(int[][] result){
    for (int i=0; i < result.length;i++){
        System.out.println(Arrays.toString(result[i]));

    }
}

public static void main(String[] args)
    {
        int[][] matrix = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,16}
            int[][] result = reverseColumns(matrix);
    print2DArray(result);       
    System.out.println()
        };

output is :

[13, 14, 15, 16]
[9, 10, 11, 12]
[5, 6, 7, 8]
[1, 2, 3, 4]

Upvotes: 0

Views: 9179

Answers (3)

brain storm
brain storm

Reputation: 31252

I have followed the semantics of reversal of cols and rows as what you suggested:

   1 2 3                                     7 8 9
   4 5 6    _______column reversal_______    4 5 6 (vertically reversed) 
   7 8 9                                     1 2 3   



   1 2 3                                     3 2 1
   4 5 6    _______row reversal__________    6 5 4 (horizontally reversed) 
   7 8 9                                     9 8 7   

It is possible to do both in-place: for the horizontal (row reversal) it is straightforward. For the vertical (col reversal), it needs some more understanding. Here the methods are; take an example matrix and try to follow the steps, you will understand

public static void reverseColumnsInPlace(int[][] matrix){
        for(int col = 0;col < matrix[0].length; col++){
            for(int row = 0; row < matrix.length/2; row++) {
                int temp = matrix[row][col];
                matrix[row][col] = matrix[matrix.length - row - 1][col];
                matrix[matrix.length - row - 1][col] = temp;
            }
    }
}

public static void reverseRowsInPlace(int[][] matrix){

    for(int row = 0; row < matrix.length; row++){
        for(int col = 0; col < matrix[row].length / 2; col++) {
            int temp = matrix[row][col];
            matrix[row][col] = matrix[row][matrix[row].length - col - 1];
            matrix[row][matrix[row].length - col - 1] = temp;
        }
    }
}

Upvotes: 2

mbroshi
mbroshi

Reputation: 979

This might help you with in-place exchanges:

private void swap(int[][] A, int row1, int col1, int row2, int col2) {
    int tmp = A[row1][col1];
    A[row1][col1] = A[row2][col2];
    A[row2][col2] = tmp;
}

Upvotes: -1

peter.petrov
peter.petrov

Reputation: 39437

The trick is to have a counter variable, say row,

then loop only to the middle of the array, and do

tmp = matrix[row];
matrix[row]=matrix[rows-1-row];
matrix[rows-1-row]=tmp;

where rows is the total count.

I'll let you figure out the type of tmp.

This is using just a constant auxiliary storage,
because tmp is just a reference (4 bytes in Java).

public static int[][] reverseColumns(int[][] matrix){
    int rows=matrix.length;
    int[] temp=null;
    for (int row=rows-1; row>=rows/2; row--){
        temp = matrix[row]; 
        matrix[row]=matrix[rows-1-row];
        matrix[rows-1-row] = temp;
    }
    return matrix;
}

Upvotes: 0

Related Questions