Сортування 2D вектора в C ++ | Встановіть 3 (на кількість стовпців)

Ми обговорили деякі випадки сортування 2D вектора в нижче наборі 1 та набору 2.
Сортування 2D вектора в C ++ | Встановіть 1 (за рядком і стовпцем)  
Сортування 2D вектора в C ++ | Встановіть 2 (у порядку зменшення за рядком та стовпцем)
У цій статті обговорюється більше випадків
Як було сказано в одній із статті, опублікованої цього набору, 2D вектор також може мати рядки з різною кількістю стовпців. Ця властивість на відміну від 2D масиву, в якому всі рядки мають однакову кількість стовпців.
 

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  ;   }   

Вихід: 
 

1 2 3 4 5 6 

Складність часу: O (n*m) n - кількість рядків, а m - кількість стовпців

Складність космосу: O (n*m)


Випадок 5: Сортування 2D вектора на основі №. стовпців поспіль у зростаючому порядку.
У цьому типі сортування 2D вектора сортується на основі ні. стовпця в порядку висхідного. Це досягається шляхом передачі третього аргументу за сортом () як заклик до чіткої функції, визначеної користувачем.
 

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  ;   }   

Вихід: 
 

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

Складність часу: O (nlog (n))

Складність космосу: O (n*m)


Випадок 6: Сортування 2D вектора на основі немає. стовпців поспіль у порядку зменшення.
У цьому типі сортування 2D вектора сортується на основі ні. стовпця в порядку зменшення. Це досягається шляхом передачі третього аргументу за сортом () як заклик до чіткої функції, визначеної користувачем.
 

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  ;   }   

Вихід: 
 

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

Складність часу: O (nlog (n))

Складність космосу: O (n*m)