# Row wise sorting in 2D array

Given a 2D array, sort each row of this array and print the result.

Examples:

```Input :
77 11 22 3
11 89 1 12
32 11 56 7
11 22 44 33
Output :
3 11 22 77
1 11 12 89
7 11 32 56
11 22 33 44

Input :
8 6 4 5
3 5 2 1
9 7 4 2
7 8 9 5
Output :
4 5 6 8
1 2 3 5
2 4 7 9
5 7 8 9
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (Using Bubble Sort)
Start iterating through each row of given 2D array, and sort elements of each row using efficient sorting algorithm.

## Java

 `// Java code to sort 2D matrix row-wise ` `import` `java.io.*; ` ` `  `public` `class` `Sort2DMatrix { ` ` `  `    ``static` `int` `sortRowWise(``int` `m[][]) ` `    ``{ ` `        ``// loop for rows of matrix ` `        ``for` `(``int` `i = ``0``; i < m.length; i++) { ` ` `  `            ``// loop for column of matrix ` `            ``for` `(``int` `j = ``0``; j < m[i].length; j++) { ` ` `  `                ``// loop for comparison and swapping ` `                ``for` `(``int` `k = ``0``; k < m[i].length - j - ``1``; k++) { ` `                    ``if` `(m[i][k] > m[i][k + ``1``]) { ` ` `  `                        ``// swapping of elements ` `                        ``int` `t = m[i][k]; ` `                        ``m[i][k] = m[i][k + ``1``]; ` `                        ``m[i][k + ``1``] = t; ` `                    ``} ` `                ``} ` `            ``} ` `        ``} ` ` `  `        ``// printing the sorted matrix ` `        ``for` `(``int` `i = ``0``; i < m.length; i++) { ` `            ``for` `(``int` `j = ``0``; j < m[i].length; j++) ` `                ``System.out.print(m[i][j] + ``" "``); ` `            ``System.out.println(); ` `        ``} ` ` `  `        ``return` `0``; ` `    ``} ` ` `  `    ``// driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `m[][] = { { ``9``, ``8``, ``7``, ``1` `}, ` `                      ``{ ``7``, ``3``, ``0``, ``2` `}, ` `                      ``{ ``9``, ``5``, ``3``, ``2` `}, ` `                      ``{ ``6``, ``3``, ``1``, ``2` `} }; ` `        ``sortRowWise(m); ` `    ``} ` `} `

## C#

 `// C# code to sort 2D matrix row-wise ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `static` `int` `sortRowWise(``int` `[,]m) ` `{ ` `    ``// loop for rows of matrix ` `    ``for` `(``int` `i = 0;  ` `             ``i < m.GetLength(0); i++) ` `    ``{ ` ` `  `        ``// loop for column of matrix ` `        ``for` `(``int` `j = 0;  ` `                 ``j < m.GetLength(1); j++)  ` `        ``{ ` ` `  `            ``// loop for comparison and swapping ` `            ``for` `(``int` `k = 0;  ` `                     ``k < m.GetLength(1) - j - 1; k++)  ` `            ``{ ` `                ``if` `(m[i, k] > m[i, k + 1])  ` `                ``{ ` ` `  `                    ``// swapping of elements ` `                    ``int` `t = m[i, k]; ` `                    ``m[i, k] = m[i, k + 1]; ` `                    ``m[i, k + 1] = t; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// printing the sorted matrix ` `    ``for` `(``int` `i = 0; ` `             ``i < m.GetLength(0); i++)  ` `    ``{ ` `        ``for` `(``int` `j = 0; ` `                 ``j < m.GetLength(1); j++) ` `            ``Console.Write(m[i, j] + ``" "``); ` `        ``Console.WriteLine(); ` `    ``} ` `    ``return` `0; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``int` `[,]m = {{ 9, 8, 7, 1 }, ` `                ``{ 7, 3, 0, 2 }, ` `                ``{ 9, 5, 3, 2 }, ` `                ``{ 6, 3, 1, 2 }}; ` `    ``sortRowWise(m); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```1 7 8 9
0 2 3 7
2 3 5 9
1 2 3 6
```

Method 2 (Using Library Function)
The idea is to use Arrays.sort() for every row of matrix.

## Java

 `// Java code to sort 2D matrix row-wise ` `import` `java.io.*; ` `import` `java.util.Arrays; ` ` `  `public` `class` `Sort2DMatrix { ` ` `  `    ``static` `int` `sortRowWise(``int` `m[][]) ` `    ``{ ` `        ``// One by one sort individual rows. ` `        ``for` `(``int` `i = ``0``; i < m.length; i++) ` `            ``Arrays.sort(m[i]); ` ` `  `        ``// printing the sorted matrix ` `        ``for` `(``int` `i = ``0``; i < m.length; i++) { ` `            ``for` `(``int` `j = ``0``; j < m[i].length; j++) ` `                ``System.out.print(m[i][j] + ``" "``); ` `            ``System.out.println(); ` `        ``} ` ` `  `        ``return` `0``; ` `    ``} ` ` `  `    ``// driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `m[][] = { { ``9``, ``8``, ``7``, ``1` `}, ` `                      ``{ ``7``, ``3``, ``0``, ``2` `}, ` `                      ``{ ``9``, ``5``, ``3``, ``2` `}, ` `                      ``{ ``6``, ``3``, ``1``, ``2` `} }; ` ` `  `        ``sortRowWise(m); ` `    ``} ` `} `

Output

```1 7 8 9
0 2 3 7
2 3 5 9
1 2 3 6
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Kadhir M, 29AjayKumar

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.