JAVA

PART -A

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7

PART-B

PROGRAM B1 PROGRAM B2 PROGRAM B3 PROGRAM B4 PROGRAM B5 PROGRAM B6 PROGRAM B7 PROGRAM B8 . . .

 
  
 
 
 
 2. Write a Java program to sort the elements of a square matrix. Read the order and elements of the matrix during execution. Before sorting the elements of the matrix, display the source matrix.
Sample output:
Input Matrix is:
20 2 35
4 16 7
41 3 2
output :
2,2,3
4,7,16
20,35,41

  PROGRAM :

 import java.io.*;
import java.util.*;
 class SortMatraix {     
    static int SIZE = 10;
 
    static void sortMat(int mat[][], int n)
    {
        // temporary matrix of size n^2
        int temp[] = new int[n * n];
        int k = 0;     
      
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                temp[k++] = mat[i][j];
     
            Arrays.sort(temp);
               
        k = 0;
        for (int i = 0; i <  n; i++)
            for (int j = 0; j <  n; j++)
                mat[i][j] = temp[k++];
    }     
      static void printMat(int mat[][], int n)
    {
        for (int i = 0; i <  n; i++) {
            for (int j = 0; j <  n; j++)
                System.out.print( mat[i][j] + " ");
            System.out.println();
        }
    }
       
    public static void main(String args[])
    {
        int mat[][] = { { 55, 4, 7 },
                        { 1, 32, 8 },
                        { 2, 9, 6 } };
        int n = 3;
     
        System.out.println("Original Matrix:");
        printMat(mat, n);
     
        sortMat(mat, n);
     
        System.out.println("Matrix After Sorting:");
        printMat(mat, n);
     
    }
}