# Java Program to Sort the 2D Array Across Rows

Sorting in data structures means to rearrange the given list of elements or a certain array, according to the question or precisely the given operator. This operator decides the new order of the elements. An array is a collection of certain elements that can be anything which takes up the adjacent memory locations.Here we store multiple elements of the same type together. Sorting helps us to rearrange these elements. For example, 15,8,12,28 are elements of an unsorted array and after sorting, 28,15,12,8 become a sorted array with descending order.

This article will demonstrate how we can sort a 2D array across rows with the help of a java program. We will be using three different methods to get to the solution. Firstly we will use the vector() method , secondly the library function Array.sort and lastly we will use the bubble sort.

## Algorithm for Vector() method

• Step 1 − Import all the required libraries.

• Step 2 − Print and display the matrix before sorting.

• Step 3 − Create an object of the Vector class.

• Step 4 − Add elements of the row in the vector.

• Step 5 − Sort the vector.

• Step 6 − Remove the elements from the vector.

• Step 7 −  Print and display the matrix after sorting.

• Step 8 − Repeat using a loop until all rows are sorted.

• Step 9 − Finish

## Syntax

Vector v = new Vector<>();


Functions that are used in the following program −

• removeAll() − This function is used to remove all the elements from the vector which we have made to store them for sorting.

• Vector.get(element) − get() method helps in getting an element of the vector stored at a particular place.

• Collection.sort() − This method is used to sort the vector.

## Algorithm using library function arrays.sort

• Step 1 − Import all the required libraries.

• Step 2 − One by one sort individual rows.

• Step 3 − Use function Arrays.sort().

• Step 4 − Print and display the sorted matrix.

## Syntax

Array.sort(arr[i]);


## Algorithm for Bubble Sort

• Step 1 − Import all the required libraries.

• Step 2 − Using loops to go through the row and column of the matrix.

• Step 3 − using another loop for comparison and swapping.

• Step 4 − Swapping the elements.

• Step 5 − Printing the sorted matrix.

## Syntax

begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+])
swap(arr[i], arr[i+1]
end if
end for
return arr
end BubbleSort


## Approaches

• Using Vector()

• Using Arrays,sort()

• Using Bubble sort()

## Approach 1: Vector()

### Example

import java.io.*;
import java.util.*;

public class WC {
public static void main(String[] args) throws java.lang.Exception {
int[][] arr = {
{1, 3, 8, 5, 9},
{5, 3, 1, 7, 4},
{0, 5, 9, 2, 6},
{4, 6, 3, 1, 0},
{3, 5, 8, 9, 2}
};

System.out.println("Array without sorting");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();

List<Integer> space = new ArrayList<>();

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
}

Collections.sort(space);

for (int j = 0; j < 5; j++) {
arr[i][j] = space.get(j);
}

space.clear();
}

System.out.println("Array after sorting");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}


### Output

Array without sorting
1 3 8 5 9
5 3 1 7 4
0 5 9 2 6
4 6 3 1 0
3 5 8 9 2
Array after sorting
1 3 5 8 9
1 3 4 5 7
0 2 5 6 9
0 1 3 4 6
2 3 5 8 9


Above is the output of the vector() method where we have printed the unsorted matrix first and after the program is executed, the sorted matrix is displayed.

## Approach 2: Arrays.sort()

### Example

import java.io.*;
import java.util.Arrays;

public class ArraySort {

static int sortrowWise ( int arr [][]) {

for (int i =0; i< arr.length; i++)
Arrays.sort(arr[i]);

for (int i =0; i < arr.length;i++) {

for( int j=0; j < arr[i].length; j++)

System.out.print(arr[i][j] + "");
System.out.println();
}
return 0;
}
public static void main(String args[]){
int arr[][] = {  { 9,2,6,4,5},
{ 8,3,7,0,2},
{ 5,3,8,1,2},
{ 3,5,7,1,0}
};
sortrowWise(arr);

}
}


### Output

24569
02378
12358
01357


Above is the output of the Arrays.sort method which is a sorted matrix after we used the library called Arrays.sort().

## Approach 3:Bubble Sort()

### Example

import java.io.*;

public class BubbleSort {
static int sortRowWise (int arr[][]) {

for (int i= 0; i < arr.length; i++) {

for (int j=0; j< arr[i].length; j++) {

for (int k=0; k<arr[i].length - j -1; k++ ) {
if (arr[i][k]> arr[i] [k+1]) {
int t = arr[i][k];
arr[i][k] = arr[i][k + 1];
arr[i][k + 1] = t;
}
}
}
}
for (int i=0; i< arr.length; i++) {
for (int j =0; j< arr [i].length; j++)

System.out.print(arr[i][j] + "");
System.out.println();
}
return 0;
}
public static void main (String arg[]){

int arr[][] = { { 0,3,6,9,4},
{ 7,5,3,8,1},
{ 2,5,3,9,0},
{ 1,7,4,0,2} };
sortRowWise(arr);
}
}


### Output

03469
13578
02359
01247


Above is the output of the Bubble sort method that we used to sort the matrix. Here we have used the comparison and swapping which gave us a sorted matrix.

## Conclusion

In the above article we learned how to sort the arrays across rows using different methods like Vector() method, Arrays.sort function and Bubble sort. All the methods have a unique algorithm which helps us in sorting the matrix.

Updated on: 31-Jul-2023

454 Views 