Sorting a two dimensional matrix in java

Posted By: Java Examples - 8:47 PM

Share

& Comment


Sorting  matrix 


class Sort_Matrix{
public static void main (String args[]){
 int [][] M ={ {1,2,3,4},{2,3,4,5},
                      {5,6,7,8},{6,7,8,5},{3,7,9,3} };
 for(int r=0;r<5;r++){
   for(int c=0;c<4;c++){
     System.out.print(M[r][c]+"\t ");
    }
   System.out.println();
  }
  for(int r=0;r<5;r++){
  int maxEle=M[r][0];
   for(int c=1;c<4;c++){
   if(M[r][c]>maxEle){
    int temp=maxEle;
    maxEle=M[r][c];
    M[r][c]=temp;
    }
   }
   System.out.println(
"Maximum row "+(r+1)+"element is= "+maxEle);
   }
  }
}

Output



Download 


About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

0 comments :

Post a Comment

Thank you for vising Java90.blogspot.com

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .