Triedenie 2D vektor v C ++ | Set 2 (v zostupnom poradí po riadku a stĺpci)

Diskutovali sme o niektorých prípadoch triedenia 2D vektora v nižšie uvedenej sade 1. Triedenie 2D vektor v C ++ | Sada 1 (po riadku a stĺpci) V tomto článku sa diskutuje viac prípadov Prípad 3: Zoradiť konkrétny riadok 2D vektora v zostupnom poradí Tento typ triedenia usporiada vybraný riadok 2D vektora v zostupnom poradí. Dosahuje sa to použitím triedenia () a odovzdaním iterátorov 1D vektora ako jeho argumentov. 

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  &  lt  ;     vector  &  lt  ;  int  &  gt  ;     &  gt  ;     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     &  lt  ;  &  lt  ;     &  quot  ;  The     Matrix     before     sorting     1  st     row     is  :    n  &  quot  ;;      for     (  int     i  =  0  ;     i  &  lt  ;  m  ;     i  ++  )      {      for     (  int     j  =  0  ;     j  &  lt  ;  n     ;  j  ++  )      cout     &  lt  ;  &  lt  ;     vect  [  i  ][  j  ]     &  lt  ;  &  lt  ;     &  quot  ;     &  quot  ;;      cout     &  lt  ;  &  lt  ;     endl  ;      }          // Use of 'sort()' for sorting first row      sort  (  vect  [  0  ].  rbegin  ()     vect  [  0  ].  rend  ());          // Displaying the 2D vector after sorting      cout     &  lt  ;  &  lt  ;     &  quot  ;  The     Matrix     after     sorting     1  st     row     is  :    n  &  quot  ;;      for     (  int     i  =  0  ;     i  &  lt  ;  m  ;     i  ++  )      {      for     (  int     j  =  0  ;     j  &  lt  ;  n     ;  j  ++  )      cout     &  lt  ;  &  lt  ;     vect  [  i  ][  j  ]     &  lt  ;  &  lt  ;     &  quot  ;     &  quot  ;;      cout     &  lt  ;  &  lt  ;     endl  ;      }          return     0  ;   }   

Výstup:

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  

Ten zložitosť tohto algoritmu je o (n log n), kde n je veľkosť vektora. 

Ten zložitosť tohto algoritmu je o (1), pretože sa nepoužíva žiadny ďalší priestor.


Prípad 4: Zoradiť celý 2D vektor na základe konkrétneho stĺpca v zostupnom poradí. V tomto type triedenia je 2D vektor úplne zoradený na základe zvoleného stĺpca v zostupnom poradí. Napríklad, ak je zvolený stĺpec druhý, riadok s najväčšou hodnotou v druhom stĺpci sa stane prvou hodnotou prvého riadku v druhom stĺpci v druhom stĺpci sa stane druhým riadkom a tak ďalej. {3 5 1} {4 8 6} {7 2 9}; Po zoradení tejto matice podľa druhého stĺpca dostaneme {4 8 6} // riadok s najväčšou hodnotou v druhom stĺpci {3 5 1} // riadok s druhou najväčšou hodnotou v druhom stĺpci {7 2 9} Toto sa dosiahne odovzdaním tretieho argumentu v triedení () ako volania definovanej explicitnej funkcie. 

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  &  lt  ;  int  &  gt  ;  &  amp  ;     v1        const     vector  &  lt  ;  int  &  gt  ;  &  amp  ;     v2     )     {      return     v1  [  1  ]     &  gt  ;     v2  [  1  ];   }       int     main  ()   {      // Initializing 2D vector 'vect' with      // values      vector  &  lt  ;     vector  &  lt  ;  int  &  gt  ;     &  gt  ;     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     &  lt  ;  &  lt  ;     &  quot  ;  The     Matrix     before     sorting     is  :    n  &  quot  ;;      for     (  int     i  =  0  ;     i  &  lt  ;  m  ;     i  ++  )      {      for     (  int     j  =  0  ;     j  &  lt  ;  n     ;  j  ++  )      cout     &  lt  ;  &  lt  ;     vect  [  i  ][  j  ]     &  lt  ;  &  lt  ;     &  quot  ;     &  quot  ;;      cout     &  lt  ;  &  lt  ;     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     &  lt  ;  &  lt  ;     &  quot  ;  The     Matrix     after     sorting     is  :    n  &  quot  ;;      for     (  int     i  =  0  ;     i  &  lt  ;  m  ;     i  ++  )      {      for     (  int     j  =  0  ;     j  &  lt  ;  n     ;  j  ++  )      cout     &  lt  ;  &  lt  ;     vect  [  i  ][  j  ]     &  lt  ;  &  lt  ;     &  quot  ;     &  quot  ;;      cout     &  lt  ;  &  lt  ;     endl  ;      }      return     0  ;   }   

Výstup:

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  

Ten zložitosť tohto algoritmu je O (nLogn) kde n je počet prvkov v 2D vektore. Je to kvôli použitiu funkcie zoradenia (), ktorá beží v čase O (nnogn).

Ten zložitosť tohto algoritmu je O (1) pretože sa nepoužívajú žiadne ďalšie dátové štruktúry.