Array-promo
Matrix-promo

Question

Program sort matrix in descending order.

				
					Matrix =  [5, 2, 7]
          [9, 6, 4]
          [3, 8, 1]
          
SORTED MATRIX
9  8  7
6  5  4
3  2  1
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class SortMatrixInDescendingOrder  
{  
    public static void main(String[] args)
    {  
        int row=0, col=0,i=0,j=0,k=0,l=0,temp=0;  
        int arr[][];
        
        
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE ROW OF MATRIX");
        row=sc.nextInt();
        System.out.println("ENTER THE COLUMN OF MATRIX");
        col=sc.nextInt();

        arr=new int[row][col];
        System.out.println("ENTER THE ELEMENTS IN MATRIX");
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                arr[i][j]=sc.nextInt();
            }
        }
            
        System.out.println("UNSORTED MATRIX ");
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }    
        
        

        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                for(k=0;k< row;k++)
                {
                    for(l=0;l< col;l++)
                    {
                        if(arr[i][j]> arr[k][l])
                        {
                            temp=arr[i][j];
                            arr[i][j]=arr[k][l];
                            arr[k][l]=temp;
                        }
                    }
                }
                    
            }
                
        }
        
        System.out.println("SORTED MATRIX ");
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
            
        
        
    }  
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *