Sort 2D array lexicographically
Given a 2D array arr[] having N rows of variable size, the task is to sort the array in lexicographical order i.e., sort each row lexicographically and then sort those sorted rows.
Examples:
Input: arr[][] = { {23}, {59}, {23, 59} }
Output: { {23}, {23, 59}, {59} }
Explanation: The rows are sorted lexicographically.
Here the row {23, 59} is lexicographically smaller than {59}
because the first element (23) is smaller than 59.
Though {23} and {23, 59} both have 23 as the first element but {23} has only one element.
Hence {23} is lexicographically smaller than {23, 59}
Input: arr[][] = { {3, 2, 5, 6}, {1, 2, 3}, {6, 3}, {5, 4, 2} }
Output: { {1, 2, 3}, {2, 3, 5, 6}, {2, 4, 5}, {3, 6} }
Input: arr[][] = { {3, 2, 5, 6}, {, 2, 3, 5}, {2, 4, 2, 1}, {6, 3, 7, 8} }
Output: { {1, 2, 2, 4}, {1, 2, 3, 5}, {2, 3, 5, 6}, {3, 6, 7, 8} }
Approach: The idea to solve the problem is as follows:
Smallest lexicographical order can be obtained by sorting the elements of each row and first then sorting the whole 2D array based on the lexicographical order of elements in each row.
Follow the below steps to solve this problem:
- First, lexicographically sort every row of the given 2D array.
- Sort the whole 2D array based on the lexicographic ordering of the elements of each row. The row which is lexicographically smaller will arrive first in the sorted matrix..
- Print the 2D array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sort_lexicographically(vector<vector< int > >& arr)
{
for ( int i = 0; i < arr.size(); ++i) {
sort(arr[i].begin(), arr[i].end());
}
sort(arr.begin(), arr.end());
}
int main()
{
vector<vector< int > > arr
= { { 3, 2, 5, 6 }, { 1, 2, 3 }, { 5, 4, 2 }, { 6, 3 }, { 9, 99 }, { 6, 3, 2 } };
sort_lexicographically(arr);
for ( int i = 0; i < arr.size(); ++i) {
for ( int j = 0; j < arr[i].size();
++j) {
cout << arr[i][j] << " " ;
}
cout << endl;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void sort_lexicographically( int arr[][])
{
for ( int i = 0 ; i < arr.length; ++i) {
Arrays.sort(arr[i]);
}
Arrays.sort(arr,
(a, b) -> Integer.compare(a[ 0 ], b[ 0 ]));
}
public static void main(String[] args)
{
int arr[][]
= { { 3 , 2 , 5 , 6 }, { 1 , 2 , 3 }, { 6 , 3 },
{ 9 , 99 }, { 6 , 3 , 2 }, { 5 , 4 , 2 } };
sort_lexicographically(arr);
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();
}
}
}
|
Python3
def sort_lexicographically(arr) :
for i in range ( 0 , len (arr)) :
arr[i].sort()
arr.sort()
if __name__ = = "__main__" :
arr = [[ 3 , 2 , 5 , 6 ], [ 1 , 2 , 3 ], [ 5 , 4 , 2 ], [ 6 , 3 ], [ 9 , 99 ], [ 6 , 3 , 2 ]]
sort_lexicographically(arr)
for i in range ( 0 , len (arr)) :
for j in range ( 0 , len (arr[i])) :
print (arr[i][j] , end = " " )
print ()
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
public static void sort_lexicographically( int [][] arr)
{
for ( int i = 0 ; i < arr.Length ; ++i) {
Array.Sort(arr[i]);
}
Array.Sort(arr, new comp());
}
public static void Main( string [] args){
int [][] arr = new int [][]{
new int []{ 3, 2, 5, 6 },
new int []{ 1, 2, 3 },
new int []{ 6, 3 },
new int []{ 9, 99 },
new int []{ 6, 3, 2 },
new int []{ 5, 4, 2 }
};
sort_lexicographically(arr);
for ( int i = 0 ; i < arr.Length ; ++i) {
for ( int j = 0 ; j < arr[i].Length ; ++j) {
Console.Write(arr[i][j] + " " );
}
Console.WriteLine( "" );
}
}
}
class comp : IComparer< int []>{
public int Compare( int [] a1, int [] a2){
return a1[0]-a2[0];
}
}
|
Javascript
<script>
function sort_lexicographically(arr)
{
for ( var i = 0; i < arr.length; i++)
arr[i].sort();
arr.sort();
}
arr = [ [ 3, 2, 5, 6 ], [ 1, 2, 3 ], [ 5, 4, 2 ],
[ 6, 3 ], [ 9, 99 ], [ 6, 3, 2 ]];
sort_lexicographically(arr);
for ( var i = 0; i < arr.length; ++i)
{
for ( var j = 0; j < arr[i].length; ++j)
{
document.write(arr[i][j] + " " );
}
document.write( "\n" );
}
</script>
|
Output
1 2 3
2 3 5 6
2 3 6
2 4 5
3 6
9 99
Time Complexity: O(N*M*log(M)), Where N is the number of rows, M is the maximum size of a row in arr
Auxiliary Space: O(1)
Last Updated :
17 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...