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
Method 1 (Using Bubble Sort): Start iterating through each row of the given 2D array, and sort elements of each row using an efficient sorting algorithm
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void sortRowWise( int m[][4],
int r, int c)
{
for ( int i = 0; i < r; i++)
{
for ( int j = 0; j < c; j++)
{
for ( int k = 0; k < c - j - 1; k++)
{
if (m[i][k] > m[i][k + 1])
{
swap(m[i][k], m[i][k + 1]);
}
}
}
}
for ( int i = 0; i < r; i++)
{
for ( int j = 0; j < c; j++)
cout << m[i][j] << " " ;
cout << endl;
}
}
int main()
{
int m[][4] = {{9, 8, 7, 1},
{7, 3, 0, 2},
{9, 5, 3, 2},
{6, 3, 1, 2}};
int c = sizeof (m[0]) / sizeof (m[0][0]);
int r = sizeof (m) / sizeof (m[0]);
sortRowWise(m, r, c);
return 0;
}
|
Java
import java.io.*;
public class Sort2DMatrix {
static int sortRowWise( int m[][])
{
for ( int i = 0 ; i < m.length; i++) {
for ( int j = 0 ; j < m[i].length; j++) {
for ( int k = 0 ; k < m[i].length - j - 1 ; k++) {
if (m[i][k] > m[i][k + 1 ]) {
int t = m[i][k];
m[i][k] = m[i][k + 1 ];
m[i][k + 1 ] = t;
}
}
}
}
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 ;
}
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);
}
}
|
Python3
def sortRowWise(m):
for i in range ( len (m)):
for j in range ( len (m[i])):
for k in range ( len (m[i]) - j - 1 ):
if (m[i][k] > m[i][k + 1 ]):
t = m[i][k]
m[i][k] = m[i][k + 1 ]
m[i][k + 1 ] = t
for i in range ( len (m)):
for j in range ( len (m[i])):
print (m[i][j], end = " " )
print ()
m = [[ 9 , 8 , 7 , 1 ],[ 7 , 3 , 0 , 2 ],[ 9 , 5 , 3 , 2 ],[ 6 , 3 , 1 , 2 ]]
sortRowWise(m)
|
C#
using System;
class GFG
{
static int sortRowWise( int [,]m)
{
for ( int i = 0;
i < m.GetLength(0); i++)
{
for ( int j = 0;
j < m.GetLength(1); j++)
{
for ( int k = 0;
k < m.GetLength(1) - j - 1; k++)
{
if (m[i, k] > m[i, k + 1])
{
int t = m[i, k];
m[i, k] = m[i, k + 1];
m[i, k + 1] = t;
}
}
}
}
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;
}
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);
}
}
|
Javascript
<script>
function sortRowWise(m)
{
for (let i = 0; i < m.length; i++) {
for (let j = 0; j < m[i].length; j++) {
for (let k = 0; k < m[i].length - j - 1; k++) {
if (m[i][k] > m[i][k + 1]) {
let t = m[i][k];
m[i][k] = m[i][k + 1];
m[i][k + 1] = t;
}
}
}
}
for (let i = 0; i < m.length; i++) {
for (let j = 0; j < m[i].length; j++)
document.write(m[i][j] + " " );
document.write( "<br/>" );
}
return 0;
}
let m = [[ 9, 8, 7, 1 ],
[ 7, 3, 0, 2 ],
[ 9, 5, 3, 2 ],
[ 6, 3, 1, 2 ]];
sortRowWise(m);
</script>
|
Output
1 7 8 9
0 2 3 7
2 3 5 9
1 2 3 6
Time Complexity: O(r*c*max(r,c))
Auxiliary Space: O(1), since no extra space has been taken.
Method 2 (Using Library Function): The idea is to use Arrays.sort() for every row of the matrix.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define M 4
#define N 4
int sortRowWise( int m[M][N])
{
for ( int i = 0; i < M; i++)
sort(m[i], m[i] + N);
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
cout << (m[i][j]) << " " ;
cout << endl;
}
}
int main()
{
int m[M][N] = {{9, 8, 7, 1},
{7, 3, 0, 2},
{9, 5, 3, 2},
{6, 3, 1, 2}};
sortRowWise(m);
}
|
Java
import java.io.*;
import java.util.Arrays;
public class Sort2DMatrix {
static int sortRowWise( int m[][])
{
for ( int i = 0 ; i < m.length; i++)
Arrays.sort(m[i]);
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 ;
}
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);
}
}
|
Python3
def sortRowWise(m):
for i in range ( len (m)):
m[i].sort()
for i in range ( len (m)):
for j in range ( len (m[i])):
print (m[i][j], end = " " )
print ()
return 0
m = [[ 9 , 8 , 7 , 1 ],[ 7 , 3 , 0 , 2 ],[ 9 , 5 , 3 , 2 ],[ 6 , 3 , 1 , 2 ]]
sortRowWise(m)
|
C#
using System;
using System.Collections.Generic;
class Sort2DMatrix{
public static int [] GetRow( int [,] matrix,
int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new int [rowLength];
for ( var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
static int sortRowWise( int [,]m)
{
for ( int i = 0;
i < m.GetLength(0); i++)
{
for ( int k = 0;
k < m.GetLength(1); k++)
for ( int j = 0;
j < m.GetLength(1) - k - 1; j++)
if (m[i, j] > m[i, j + 1])
{
int temp = m[i, j];
m[i, j] = m[i, j + 1];
m[i, j + 1] = temp;
}
}
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;
}
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);
}
}
|
Javascript
<script>
function GetRow(matrix, row)
{
var rowLength = matrix[0].length;
var rowVector = new int[rowLength];
for ( var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row][i];
return rowVector;
}
function sortRowWise(m)
{
for ( var i = 0;
i < m.length; i++)
{
for ( var k = 0;
k < m[0].length; k++)
for ( var j = 0;
j < m[0].length - k - 1; j++)
if (m[i][j] > m[i][j + 1])
{
var temp = m[i][j];
m[i][j] = m[i][j + 1];
m[i][j + 1] = temp;
}
}
for ( var i = 0;
i < m.length; i++)
{
for ( var j = 0;
j < m[0].length; j++)
document.write(m[i][j] + " " );
document.write( "<br>" );
}
return 0;
}
var m = [[9, 8, 7, 1],
[7, 3, 0, 2],
[9, 5, 3, 2],
[6, 3, 1, 2]];
sortRowWise(m);
</script>
|
Output
1 7 8 9
0 2 3 7
2 3 5 9
1 2 3 6
Time Complexity: O(r*c*log(c))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!