Display matrix elements in a spiral order.

Problem :  We have to display elements of matrix in spiral order. For the given matrix mat(4x4).
               

 In Output order of elements should be  : 1  2  3  11  12  13  9  8  7  4   5  6
 Note : solution proposed is valid for matrix of any order  (mxn) 


Solution :  First we traverse row wise  in right direction and then column wise in bottom direction,then again row wise in left direction and again column wise  in  upward direction, this way we have completed one complete cycle of matrix.We repeat same process until we have row and column elements left.

Algorithm :
     Initialize row=0 and col = 0, row and column order is mxn  
       Loop until row < m & col < n (row elements and column elements exist)  
       First : Iterate row-wise first   
           for i=col to n  
             print arr(row,i)  
           Increment row.  
       second : Iterate Coulumn in bottom direction from top right  
                      for i = row to m   
                       print arr(i,n)  
                decrement n.   
            third : Iterate row in left direction from rith to left  
                      for i=n-1 to col  
                        print arr(m-1,i)  
                      decrement m.  
            four : Iterate column from bottom to top  
                      for i=m-1 to row  
                       print arr(i,col)   
                       increment col.  
            repeat from step first.  

Now we will write sample code in java for desired result. Sample running program is as follows :
 public class SpiralPatternGeneration {  
   public SpiralPatternGeneration() {  
     super();  
   }  
   public static void spiralPatternGeneration(int[][] mat, int m, int n) {  
     int col = 0, row = 0;  
     int i;  
     while (row < m && col < n) {  
       for (i = col; i < n; i++) {  
         System.out.print(mat[row][i] + " ");  
       }  
       row++;  
       for (i = row; i < m; i++) {  
         System.out.print(mat[i][n - 1]+ " ");  
       }  
       n--;  
       if (row < m) {  
         for (i = n - 1; i >= col; i--) {  
           System.out.print(mat[m - 1][i] + " ");  
         }  
         m--;  
       }  
       if (col < n) {  
         for (i = m - 1; i >= row; i--) {  
           System.out.print(mat[i][col] + " ");  
         }  
         col++;  
       }  
       System.out.println(" ");  
     }  
   }  
   public static void main(String[] args) {  
     SpiralPatternGeneration spiralPattern = new SpiralPatternGeneration();  
     int[][] m = { { 1, 2, 3,11 }, { 4, 5, 6,12 }, { 7, 8, 9 ,13} };  
     spiralPatternGeneration(m, 3, 4);  
   }  
 }  

Sample output  when we execute the above program comes as :
1 2 3 11 12 13 9 8 7 4
5 6
First line(1 to 4 )  is outcome of one round of spiral movement
second line(5-6)  is outcome of second iteration.
-------------------------------------------------------------------------

1 Comments

Previous Post Next Post