Sunday , 16 December 2018
Latest Posts
Home » Array Related Programs » How to Sort a Two Dimensional (2D) Array

# How to Sort a Two Dimensional (2D) Array

To be frank, the above topic has always been a nightmare for computer students. They know how to sort a one-dimensional (1D) array using any of the standard sorting techniques like Bubble Sort, Selection Sort, Insertion Sort etc, but they face problem when they are told to sort a two-dimensional array.

This short article will cover few simple but smart steps which we can use to sort a 2D array. Now, there are two different approaches which you can take while sorting a given 2D array.

#### Method 1: Convert the 2D array into 1D and then sort that 1D array.

Below are the steps that follow:

Step 1: Input the 2D array of ‘m’ rows and ‘n’ columns.

Example:

Step 2: Print the original array.

Step 3: Create a 1D array of size ‘m*n

Step 4: Save all elements of 2D array into 1D array (i.e. converting a 2D array into a 1D array)

Step 5: Sort the 1D array you just created using any standard sorting technique

Step 6: Save the elements of the sorted 1D array back to the 2D array.

Below is the programming code on how to sort a two dimensional array in ascending order using the Selection Sort technique.

#### Programming Code:

```/**
* The class Sort2D_Method1 inputs a two dimensional array and sorts it in ascending order
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Sort2D_Method1
{
public static void main(String args[])throws IOException
{

System.out.print("Enter the no. of  rows: "); //inputting number of rows
System.out.print("Enter the no. of columns: "); //inputting number of columns

int A[][]=new int[m][n]; //creating a 2D array

/* Inputting the 2D Array */

for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print("Enter the elements: ");
}
}

/* Printing the original 2D Array */

System.out.println("The original array:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}

/* Saving the 2D Array into a 1D Array */

int B[]=new int[m*n]; //creating a 1D Array of size 'r*c'
int x = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
B[x] = A[i][j];
x++;
}
}

/*Sorting the 1D Array in Ascending Order*/

int t=0;
for(int i=0; i<(m*n)-1; i++)
{
for(int j=i+1; j<(m*n); j++)
{
if(B[i]>B[j])
{
t=B[i];
B[i]=B[j];
B[j]=t;
}
}
}

/*Saving the sorted 1D Array back into the 2D Array */

x = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
A[i][j] = B[x];
x++;
}
}

/* Printing the sorted 2D Array */

System.out.println("The Sorted Array:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
}
```

#### Method 2: Sort the 2D array directly

Below is the programming code on how to sort a two dimensional array without converting it to any 1D array.

#### Programming Code:

```/**
* The class Sort2D_Method2 inputs a two dimensional array and sorts it in ascending order
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Sort2D_Method2
{
public static void main(String args[])throws IOException
{

System.out.print("Enter the no. of  rows: "); //inputting number of rows
System.out.print("Enter the no. of columns: "); //inputting number of columns

int A[][]=new int[m][n]; //creating a 2D array

/* Inputting the 2D Array */

for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print("Enter the elements: ");
}
}

/* Printing the original 2D Array */

System.out.println("The original array:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}

/* Sorting the 2D Array */

int t=0;
for(int x=0;x<m;x++)
{
for(int y=0;y<n;y++)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(A[i][j]>A[x][y])
{
t=A[x][y];
A[x][y]=A[i][j];
A[i][j]=t;
}
}
}
}
}

/* Printing the sorted 2D Array */

System.out.println("The Sorted Array:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
}
```

## [Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

1. Thank you ðŸ™‚

2. Can we do the same program by using binary sort? If yes, then please show the code. Thanks in advance.
P.S. -You’re doing a great job! Keep it up!

3. I needed help with finding the smallest and the greatest number in a 2-D array, and have to print its index too? How can I go about it?
Thank you.

• use this code after inputting the 2D array in A[][] of ‘r’ rows and ‘c’ columns.

int max=A[0][0], min=A[0][0], rmax=0, rmin=0, cmax=0, cmin=0;
for(int i=0; imax)
{
max=A[i][j];
rmax=i;
cmax=j;
}
/* finding minimum element and its row and column index */
if(A[i][j]