logo

Razvrščanje 2D vektorja v C ++ | Set 2 (v padajočem vrstnem redu po vrstici in stolpcu)

Razpravljali smo o nekaterih primerih razvrščanja 2D vektorja v spodnjem nizu 1. Razvrščanje 2D vektorja v C ++ | Set 1 (po vrstici in stolpcu) V tem članku je obravnavanih več primerov Primer 3: Razvrstiti določeno vrstico 2D vektorja v padajočem vrstnem redu Ta vrsta razvrščanja razporedi izbrano vrstico 2D vektorja v padajočem vrstnem redu. To dosežemo z uporabo sorta () in prenašanjem iteratorjev 1D vektorja kot njene argumente. 

CPP
// C++ code to demonstrate sorting of a // row of 2D vector in descending order #include   #include // for 2D vector #include   // for sort() using namespace std;   int main() {  // Initializing 2D vector 'vect' with  // values  vector< vector<int> > vect{{3 5 1}  {4 8 6}  {7 2 9}};  // Number of rows;  int m = vect.size();    // Number of columns (Assuming all rows  // are of same size). We can have different  // sizes though (like Java).  int n = vect[0].size();    // Displaying the 2D vector before sorting  cout << "The Matrix before sorting 1st row is:n";  for (int i=0; i<m; i++)  {  for (int j=0; j<n ;j++)  cout << vect[i][j] << " ";  cout << endl;  }    // Use of 'sort()' for sorting first row  sort(vect[0].rbegin() vect[0].rend());    // Displaying the 2D vector after sorting  cout << "The Matrix after sorting 1st row is:n";  for (int i=0; i<m; i++)  {  for (int j=0; j<n ;j++)  cout << vect[i][j] << " ";  cout << endl;  }    return 0; } 

Izhod:



The Matrix before sorting 1st row is: 3 5 1 4 8 6 7 2 9 The Matrix after sorting 1st row is: 5 3 1 4 8 6 7 2 9 

The časovna zapletenost tega algoritma je o (n log n), kjer je n velikost vektorja. 

The vesoljska kompleksnost tega algoritma je O (1), saj se dodatni prostor ne uporablja.


Primer 4: Razvrstiti celoten 2D vektor na podlagi določenega stolpca v padajočem vrstnem redu. Pri tej vrsti razvrščanja je 2D vektor v celoti razvrščen na podlagi izbranega stolpca v padajočem vrstnem redu. Na primer, če je izbrani stolpec drugič, vrstica z največjo vrednostjo v drugem stolpcu postane druga največja vrednost v drugem stolpcu v drugem stolpcu postane druga vrstica in tako naprej. {3 5 1} {4 8 6} {7 2 9}; Po razvrščanju te matrice po drugem stolpcu dobimo {4 8 6} // vrstico z največjo vrednostjo v drugem stolpcu {3 5 1} // vrstico z drugo največjo vrednostjo v drugem stolpcu {7 2 9} To je doseženo s prenašanjem tretjega argumenta v sortiranju () kot klic, ki je opredeljena uporabniku, ki je opredeljena izrecno funkcijo. 



CPP
// C++ code to demonstrate sorting of a // 2D vector on basis of a column 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 particular column in  // descending order bool sortcol( const vector<int>& v1  const vector<int>& v2 ) {  return v1[1] > v2[1]; }   int main() {  // Initializing 2D vector 'vect' with  // values  vector< vector<int> > vect{{3 5 1}  {4 8 6}  {7 2 9}};    // Number of rows;  int m = vect.size();    // Number of columns (Assuming all rows  // are of same size). We can have different  // sizes though (like Java).  int n = vect[0].size();    // Displaying the 2D vector before sorting  cout << "The Matrix before sorting is:n";  for (int i=0; i<m; i++)  {  for (int j=0; j<n ;j++)  cout << vect[i][j] << " ";  cout << endl;  }     // Use of 'sort()' for sorting on basis  // of 2nd column in descending order  sort(vect.begin() vect.end()sortcol);    // Displaying the 2D vector after sorting  cout << "The Matrix after sorting is:n";  for (int i=0; i<m; i++)  {  for (int j=0; j<n ;j++)  cout << vect[i][j] << " ";  cout << endl;  }  return 0; } 

Izhod:

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

The časovna zapletenost tega algoritma je O (nlogn) kjer je n število elementov v 2D vektorju. To je posledica uporabe funkcije sorta (), ki deluje v času O (nlogn).

The vesoljska kompleksnost tega algoritma je O (1) Ker se dodatne podatkovne strukture ne uporabljajo.