Computer Science, asked by BrainlyProgrammer, 2 months ago

Java Storm!!

>>How to sort 2D array?
Also, please provide sample program which sorts 2D array.

Thanks, Do not spam​

Answers

Answered by anindyaadhikari13
5

\texttt{\textsf{\large{\underline{Solution}:}}}

There are two ways in which a matrix (or 2D Array) can be sorted.

  • Sorting row-wise and,
  • Sorting column-wise.

Consider a matrix A. -

\left[\begin{array}{ccc}\tt9&\tt8&\tt7\\ \tt6&\tt5&\tt4\\ \tt3&\tt2&\tt1\end{array}\right]

If we sort row-wise, we get -

\left[\begin{array}{ccc}\tt7&\tt8&\tt9\\ \tt4&\tt5&\tt6\\ \tt1&\tt2&\tt3\end{array}\right]

If we sort the same matrix column-wise, we get -

\left[\begin{array}{ccc}\tt3&\tt2&\tt1\\ \tt6&\tt5&\tt4\\ \tt9&\tt8&\tt7\end{array}\right]

⊕ So here, we will use our own logic to solve this problem.

\texttt{\textsf{\large{\underline{Approach}:}}}

Here comes my approach.

import java.util.*;

public class Sort2DArray{

   public static void main(String s[]){

       Scanner sc=new Scanner(System.in);

       int rows,cols,i,j,k;

       System.out.print("How many rows: ");

       rows=sc.nextInt();

       System.out.print("How many cols: ");

       cols=sc.nextInt();

       int a[][]=new int[rows][cols];

       System.out.println("Enter the array elements...");

       for(i=0;i<rows;i++){

           for(j=0;j<cols;j++){

               System.out.printf("a[%d][%d] >> ",i,j);

               a[i][j]=sc.nextInt();

           }

       }

       System.out.println("\n1. Sort Row-wise.");

       System.out.println("2. Sort Column-wise.");

       System.out.print("Enter your choice: ");

       switch(sc.nextInt())

           case 1:

           System.out.println("\nGiven Array:");

           displayMatrix(a);  

           for(int x[]:a)

               Arrays.sort(x);

           System.out.println("\nSorted Array:");

           displayMatrix(a);

           break;

           case 2:

           System.out.println("\nGiven Array:");

           displayMatrix(a);

           for(i=0;i<rows;i++){

               for(j=0;j<cols;j++){

                   for(k=i+1;k<rows;k++){

                       if(a[i][j]>a[k][j]){

                           int temp=a[i][j];

                           a[i][j]=a[k][j];

                           a[k][j]=temp;

                       }

                   }

               }

           }

           System.out.println("\nSorted Array:");

           displayMatrix(a);

           break;

           default: System.out.println("Invalid Choice. Program Terminated.");

       }

   }

   static void displayMatrix(int a[][]){

       for(int x[]:a){

           for(int y:x)

               System.out.print(y+" ");

           System.out.println();

       }

   }

}

See the attachment for output.

Attachments:
Answered by Manav1235
0

Make the 2D array into a separate simple (1D) array (STEP 1). Then use the Arrays. sort() method to sort the simple array (STEP 2). Then set each space of the 2D array to be the number of columns across (X-coordinate where the space will be changed) multiplied by the number of spaces per row in the 2D array.

Similar questions