Ми обговорили деякі випадки сортування 2D вектора в нижче наборі 1 та набору 2.
Сортування 2D вектора в C ++ | Встановіть 1 (за рядком і стовпцем)
Сортування 2D вектора в C ++ | Встановіть 2 (у порядку зменшення за рядком та стовпцем)
У цій статті обговорюється більше випадків
Як було сказано в одній із статті, опублікованої цього набору, 2D вектор також може мати рядки з різною кількістю стовпців. Ця властивість на відміну від 2D масиву, в якому всі рядки мають однакову кількість стовпців.
// 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; }
Вихід:
1 2 3 4 5 6
Складність часу: O (n*m) n - кількість рядків, а m - кількість стовпців
історія в java
Складність космосу: 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<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; }
Вихід:
приклад обрізки альфа-бета
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 вектора сортується на основі ні. стовпця в порядку зменшення. Це досягається шляхом передачі третього аргументу за сортом () як заклик до чіткої функції, визначеної користувачем.
// 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; }
Вихід:
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))
ціле число в рядок у java
Складність космосу: O (n*m)