Array-promo
Matrix-promo

Question

Program to column-wise sorting matrix in ascending order

				
					Matrix
[3, 20, 1]
[13, 5, 4]
[9, 19, 7]

SORTED MATRIX
3  5  1
9  19 4
13 20 7
				
			

Share code with your friends

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

Code

				
					import java.util.Scanner;
public class columnSortingInAscendingOrder  
{  
    public static void main(String[] args)
    {  
        int row=0, col=0,i=0,j=0,k=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++)
                {
                        if(arr[i][j] < arr[k][j])
                        {
                            temp=arr[i][j];
                            arr[i][j]=arr[k][j];
                            arr[k][j]=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 *