Sorting 2D Vector in C++ | Set 3 (By number of columns)
We have discussed some of the cases of sorting 2D vector in below set 1 and set 2.
Sorting 2D Vector in C++ | Set 1 (By row and column)
Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
More cases are discussed in this article
As mentioned in one of the article published of this set, A 2D Vector can also have rows with different number of columns. This property is unlike the 2D Array in which all rows have same number of columns.
CPP
#include<iostream>
#include<vector> // for 2D vector
using namespace std;
int main()
{
vector< vector< int > > vect{{1, 2},
{3, 4, 5},
{6}};
for ( int i=0; i<vect.size(); i++)
{
for ( int j=0; j<vect[i].size() ;j++)
cout << vect[i][j] << " ";
cout << endl;
}
return 0;
}
|
Output:
1 2
3 4 5
6
Time Complexity: O(n*m) n is the number of rows and m is the number of columns
Space Complexity: O(n*m)
Case 5 : Sorting the 2D Vector on basis of no. of columns in row in ascending order.
In this type of sorting, 2D vector is sorted on basis of a no. of column in ascending order. This is achieved by passing a third argument in “sort()” as a call to user defined explicit function.
CPP
#include<iostream>
#include<vector> // for 2D vector
#include<algorithm> // for sort()
using namespace std;
bool sizecom( const vector< int >& v1, const vector< int >& v2)
{
return v1.size() < v2.size();
}
int main()
{
vector< vector< int > > vect{{1, 2},
{3, 4, 5},
{6}};
cout << "The Matrix before sorting is:\n";
for ( int i=0; i<vect.size(); i++)
{
for ( int j=0; j<vect[i].size() ;j++)
cout << vect[i][j] << " ";
cout << endl;
}
sort(vect.begin(), vect.end(), sizecom);
cout << "The Matrix after sorting is:\n";
for ( int i=0; i<vect.size(); i++)
{
for ( int j=0; j<vect[i].size() ;j++)
cout << vect[i][j] << " ";
cout << endl;
}
return 0;
}
|
Output:
The Matrix before sorting is:
1 2
3 4 5
6
The Matrix after sorting is:
6
1 2
3 4 5
Time Complexity: O(nlog(n))
Space Complexity: O(n*m)
Case 6 : Sorting the 2D Vector on basis of no. of columns in row in descending order.
In this type of sorting, 2D vector is sorted on basis of a no. of column in descending order. This is achieved by passing a third argument in “sort()” as a call to user defined explicit function.
CPP
#include<iostream>
#include<vector> // for 2D vector
#include<algorithm> // for sort()
using namespace std;
bool sizecom( const vector< int >& v1, const vector< int >& v2)
{
return v1.size() > v2.size();
}
int main()
{
vector< vector< int > > vect{{1, 2},
{3, 4, 5},
{6}};
cout << "The Matrix before sorting is:\n";
for ( int i=0; i<vect.size(); i++)
{
for ( int j=0; j<vect[i].size() ;j++)
cout << vect[i][j] << " ";
cout << endl;
}
sort(vect.begin(), vect.end(), sizecom);
cout << "The Matrix after sorting is:\n";
for ( int i=0; i<vect.size(); i++)
{
for ( int j=0; j<vect[i].size() ;j++)
cout << vect[i][j] << " ";
cout << endl;
}
return 0;
}
|
Output:
The Matrix before sorting is:
1 2
3 4 5
6
The Matrix after sorting is:
3 4 5
1 2
6
Time Complexity: O(nlog(n))
Space Complexity: O(n*m)
Last Updated :
10 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...