logo

Sortera 2D -vektor i C ++ | Set 3 (efter antal kolumner)

Vi har diskuterat några av fallen med att sortera 2D -vektor i nedanstående set 1 och set 2.
Sortera 2D -vektor i C ++ | Set 1 (efter rad och kolumn)  
Sortera 2D -vektor i C ++ | Set 2 (i fallande ordning efter rad och kolumn)
Fler fall diskuteras i den här artikeln
Som nämnts i en av artikeln som publicerats av denna uppsättning kan en 2D -vektor också ha rader med olika antal kolumner. Den här egenskapen är till skillnad från 2D -matrisen där alla rader har samma antal kolumner.
 

CPP
// C++ code to demonstrate 2D Vector // with different no. of columns #include   #include // for 2D vector using namespace std; int main() {  // Initializing 2D vector 'vect' with  // values  vector< vector<int> > vect{{1 2}  {3 4 5}  {6}};  // Displaying the 2D vector  for (int i=0; i<vect.size(); i++)  {  //loop till the size of particular  //row  for (int j=0; j<vect[i].size() ;j++)  cout << vect[i][j] << " ";  cout << endl;  }  return 0; } 

Produktion: 
 



1 2 3 4 5 6

Tidskomplexitet: O (n*m) n är antalet rader och m är antalet kolumner

Rymdkomplexitet: O (n*m)


Fall 5: Sortera 2D -vektorn på grundval av nr. av kolumner i rad i stigande ordning.
I den här typen av sortering sorteras 2D -vektor utifrån ett nr. av kolumn i stigande ordning. Detta uppnås genom att överföra ett tredje argument i sort () som en uppmaning till användardefinierad uttrycklig funktion.
 



CPP
// C++ code to demonstrate sorting of // 2D vector on basis of no. of columns // in ascending order #include   #include // for 2D vector #include   // for sort() using namespace std; // Driver function to sort the 2D vector // on basis of a no. of columns in  // ascending order bool sizecom(const vector<int>& v1 const vector<int>& v2) {  return v1.size() < v2.size(); } int main() {  // Initializing 2D vector 'vect' with  // values  vector< vector<int> > vect{{1 2}  {3 4 5}  {6}};  // Displaying the 2D vector before sorting  cout << "The Matrix before sorting is:n";  for (int i=0; i<vect.size(); i++)  {  //loop till the size of particular  //row  for (int j=0; j<vect[i].size() ;j++)  cout << vect[i][j] << " ";  cout << endl;  }  //Use of 'sort()' for sorting on  //basis of no. of columns in  //ascending order.  sort(vect.begin() vect.end() sizecom);  // Displaying the 2D vector after sorting  cout << "The Matrix after sorting is:n";  for (int i=0; i<vect.size(); i++)  {  //loop till the size of particular  //row  for (int j=0; j<vect[i].size() ;j++)  cout << vect[i][j] << " ";  cout << endl;  }  return 0; } 

Produktion: 
 

The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 6 1 2 3 4 5 

Tidskomplexitet: O (nlog (n))

Rymdkomplexitet: O (n*m)




Fall 6: Sortera 2D -vektorn på grundval av nr. av kolumner i rad i fallande ordning.
I den här typen av sortering sorteras 2D -vektor utifrån ett nr. av kolumn i fallande ordning. Detta uppnås genom att överföra ett tredje argument i sort () som en uppmaning till användardefinierad uttrycklig funktion.
 

CPP
// C++ code to demonstrate sorting of // 2D vector on basis of no. of columns // in descending order #include   #include // for 2D vector #include   // for sort() using namespace std; // Driver function to sort the 2D vector // on basis of a no. of columns in  // descending order bool sizecom(const vector<int>& v1 const vector<int>& v2) {  return v1.size() > v2.size(); } int main() {  // Initializing 2D vector 'vect' with  // values  vector< vector<int> > vect{{1 2}  {3 4 5}  {6}};  // Displaying the 2D vector before sorting  cout << "The Matrix before sorting is:n";  for (int i=0; i<vect.size(); i++)  {  //loop till the size of particular  //row  for (int j=0; j<vect[i].size() ;j++)  cout << vect[i][j] << " ";  cout << endl;  }  //Use of 'sort()' for sorting on  //basis of no. of columns in  //descending order.  sort(vect.begin() vect.end() sizecom);  // Displaying the 2D vector after sorting  cout << "The Matrix after sorting is:n";  for (int i=0; i<vect.size(); i++)  {  //loop till the size of particular  //row  for (int j=0; j<vect[i].size() ;j++)  cout << vect[i][j] << " ";  cout << endl;  }  return 0; } 

Produktion: 
 

The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 3 4 5 1 2 6 

Tidskomplexitet: O (nlog (n))

Rymdkomplexitet: O (n*m)