Sorting Rows of Matrix in Descending Order Java

Sorting Rows of Matrix in Descending Order Java 

 
Sort 2D array in Descending Order Java

Here is  the Program to Sort 2D array in Descending Order Java

We are uploading these types of program. However, if you need fully compiled solution of any type of Java program then simply leave a comment below, we will help you at the earliest. 😊😊

 import java.util.*;  
 public class Bubble_Sort_In_Matrix_Descending_Order  
 {  
   public static void main()  
   {  
    Scanner sc=new Scanner(System.in);  
    int n=0,temp,i,j;  
    System.out.println("Enter the number of Rows");  
    int r=sc.nextInt();  
    System.out.println("Enter the number of Columns");  
    int c=sc.nextInt();  
    int arr[][]=new int[r][c];  
    int brr[]=new int[r*c];  
    System.out.println("Enter the elements of Matrix");  
     for( i=0;i<r;i++)  
     {  
       for( j=0;j<c;j++)  
       {         
         arr[i][j]=sc.nextInt();  
       }        
     }  
     System.out.println("INPUT MATRIX IS:-");  
     for(i=0;i<r;i++)  
     {  
       for(j=0;j<c;j++)  
       {   
        brr[n++]=arr[i][j];  
       System.out.print(arr[i][j]+" ");          
       }  
       System.out.println("");  
     }  
     for(int l=0;l<n-1;l++)  
     {  
       for(int k=0;k<n-1;k++)  
       {  
         if(brr[k]<brr[k+1])  
         {  
           temp=brr[k];  
           brr[k]=brr[k+1];  
           brr[k+1]=temp;  
         }  
       }  
     }  
     n=0;  
     for(i=0;i<r;i++)  
     {  
       for(j=0;j<c;j++)  
       {          
         arr[i][j]=brr[n++];          
       }       
     }  
     System.out.println("REARRANGED MATRIX IS:-");  
     for(i=0;i<r;i++)  
     {  
       for(j=0;j<c;j++)  
       {      
         System.out.print(arr[i][j]+" ");  
       }  
       System.out.println("");  
     }  
   }  
 }  
 -------------  
 OUTPUT:-  
 ------------  
 Enter the number of Rows  
 4  
 Enter the number of Columns  
 4  
 Enter the elements of Matrix  
 1  
 5  
 6  
 3  
 2  
 4  
 8  
 9  
 7  
 5  
 6  
 3  
 2  
 1  
 5  
 9  
 INPUT MATRIX IS:-  
 1 5 6 3  
 2 4 8 9  
 7 5 6 3  
 2 1 5 9  
 REARRANGED MATRIX IS:-  
 9 9 8 7  
 6 6 5 5  
 5 4 3 3  
 2 2 1 1  
 --------  

Checkout:-Bubble Sort Ascending Order Java Program

Checkout:-  Bubble Sort In Descending Order Java

Post a Comment

0 Comments