Triedenie 2D vektor v C ++ | Sada 3 (podľa počtu stĺpcov)

Diskutovali sme o niektorých prípadoch triedenia 2D vektora v nižšie uvedenej sade 1 a set 2.
Triedenie 2D vektor v C ++ | Sada 1 (po riadku a stĺpci)  
Triedenie 2D vektor v C ++ | Set 2 (v zostupnom poradí po riadku a stĺpci)
V tomto článku sa diskutuje viac prípadov
Ako je uvedené v jednom z článku uverejneného tohto súboru, 2D vektor môže mať tiež riadky s rôznym počtom stĺpcov. Táto vlastnosť je na rozdiel od 2D poľa, v ktorom majú všetky riadky rovnaký počet stĺpcov.
 

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  &  lt  ;     vector  &  lt  ;  int  &  gt  ;     &  gt  ;     vect  {{  1       2  }      {  3       4       5  }      {  6  }};      // Displaying the 2D vector      for     (  int     i  =  0  ;     i  &  lt  ;  vect  .  size  ();     i  ++  )      {      //loop till the size of particular      //row      for     (  int     j  =  0  ;     j  &  lt  ;  vect  [  i  ].  size  ()     ;  j  ++  )      cout     &  lt  ;  &  lt  ;     vect  [  i  ][  j  ]     &  lt  ;  &  lt  ;     &  quot  ;     &  quot  ;;      cout     &  lt  ;  &  lt  ;     endl  ;      }      return     0  ;   }   

Výstup: 
 

1 2 3 4 5 6 

Časová zložitosť: O (n*m) n je počet riadkov a m je počet stĺpcov

Zložitosť vesmíru: O (n*m)


Prípad 5: Triedenie 2D vektora na základe č. stĺpcov v rade vo vzostupnom poradí.
V tomto type triedenia je 2D vektor triedený na základe č. stĺpca vo vzostupnom poradí. To sa dosiahne odovzdaním tretieho argumentu v Sort () ako volania na explicitnú funkciu definovanú používateľom.
 

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  &  lt  ;  int  &  gt  ;  &  amp  ;     v1       const     vector  &  lt  ;  int  &  gt  ;  &  amp  ;     v2  )   {      return     v1  .  size  ()     &  lt  ;     v2  .  size  ();   }   int     main  ()   {      // Initializing 2D vector 'vect' with      // values      vector  &  lt  ;     vector  &  lt  ;  int  &  gt  ;     &  gt  ;     vect  {{  1       2  }      {  3       4       5  }      {  6  }};      // Displaying the 2D vector before sorting      cout     &  lt  ;  &  lt  ;     &  quot  ;  The     Matrix     before     sorting     is  :    n  &  quot  ;;      for     (  int     i  =  0  ;     i  &  lt  ;  vect  .  size  ();     i  ++  )      {      //loop till the size of particular      //row      for     (  int     j  =  0  ;     j  &  lt  ;  vect  [  i  ].  size  ()     ;  j  ++  )      cout     &  lt  ;  &  lt  ;     vect  [  i  ][  j  ]     &  lt  ;  &  lt  ;     &  quot  ;     &  quot  ;;      cout     &  lt  ;  &  lt  ;     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     &  lt  ;  &  lt  ;     &  quot  ;  The     Matrix     after     sorting     is  :    n  &  quot  ;;      for     (  int     i  =  0  ;     i  &  lt  ;  vect  .  size  ();     i  ++  )      {      //loop till the size of particular      //row      for     (  int     j  =  0  ;     j  &  lt  ;  vect  [  i  ].  size  ()     ;  j  ++  )      cout     &  lt  ;  &  lt  ;     vect  [  i  ][  j  ]     &  lt  ;  &  lt  ;     &  quot  ;     &  quot  ;;      cout     &  lt  ;  &  lt  ;     endl  ;      }      return     0  ;   }   

Výstup: 
 

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

Časová zložitosť: O (nlog (n))

Zložitosť vesmíru: O (n*m)


Prípad 6: Triedenie 2D vektora na základe č. stĺpcov v riadku v zostupnom poradí.
V tomto type triedenia je 2D vektor triedený na základe č. stĺpca v zostupnom poradí. To sa dosiahne odovzdaním tretieho argumentu v Sort () ako volania na explicitnú funkciu definovanú používateľom.
 

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  &  lt  ;  int  &  gt  ;  &  amp  ;     v1       const     vector  &  lt  ;  int  &  gt  ;  &  amp  ;     v2  )   {      return     v1  .  size  ()     &  gt  ;     v2  .  size  ();   }   int     main  ()   {      // Initializing 2D vector 'vect' with      // values      vector  &  lt  ;     vector  &  lt  ;  int  &  gt  ;     &  gt  ;     vect  {{  1       2  }      {  3       4       5  }      {  6  }};      // Displaying the 2D vector before sorting      cout     &  lt  ;  &  lt  ;     &  quot  ;  The     Matrix     before     sorting     is  :    n  &  quot  ;;      for     (  int     i  =  0  ;     i  &  lt  ;  vect  .  size  ();     i  ++  )      {      //loop till the size of particular      //row      for     (  int     j  =  0  ;     j  &  lt  ;  vect  [  i  ].  size  ()     ;  j  ++  )      cout     &  lt  ;  &  lt  ;     vect  [  i  ][  j  ]     &  lt  ;  &  lt  ;     &  quot  ;     &  quot  ;;      cout     &  lt  ;  &  lt  ;     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     &  lt  ;  &  lt  ;     &  quot  ;  The     Matrix     after     sorting     is  :    n  &  quot  ;;      for     (  int     i  =  0  ;     i  &  lt  ;  vect  .  size  ();     i  ++  )      {      //loop till the size of particular      //row      for     (  int     j  =  0  ;     j  &  lt  ;  vect  [  i  ].  size  ()     ;  j  ++  )      cout     &  lt  ;  &  lt  ;     vect  [  i  ][  j  ]     &  lt  ;  &  lt  ;     &  quot  ;     &  quot  ;;      cout     &  lt  ;  &  lt  ;     endl  ;      }      return     0  ;   }   

Výstup: 
 

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

Časová zložitosť: O (nlog (n))

Zložitosť vesmíru: O (n*m)