Razgovarali smo o nekim slučajevima razvrstavanja 2D vektora u nižoj setu 1 i set 2.
Sortiranje 2D vektora u C ++ | Postavite 1 (redom i stupcem)
Sortiranje 2D vektora u C ++ | Postavite 2 (u silaznom redoslijedu prema retku i stupcu)
U ovom se članku raspravlja o više slučajeva
Kao što je spomenuto u jednom od članka objavljenog ovog skupa, 2D vektor može imati i retke s različitim brojem stupaca. Ovo svojstvo je za razliku od 2D niza u kojem svi redovi imaju isti broj stupaca.
// 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; }
Izlaz:
1 2 3 4 5 6
Vremenska složenost: O (n*m) n je broj redaka, a m je broj stupaca
Složena složenost: O (n*m)
stablo binarnog pretraživanja]
Slučaj 5: Razvrstavanje 2D vektora na osnovu br. stupaca u redu uzlaznim redoslijedom.
U ovoj vrsti sortiranja 2D vektor je sortiran na osnovu br. stupca u uzlaznom redoslijedu. To se postiže prenošenjem trećeg argumenta u sort () kao pozivu na korisnika definiranu eksplicitnu funkciju.
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; }
Izlaz:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 6 1 2 3 4 5
Vremenska složenost: O (nlog (n))
primjer stabla binarnog pretraživanja
Složena složenost: O (n*m)
Slučaj 6: Razvrstavanje 2D vektora na osnovu br. od stupaca u redu u silaznom redoslijedu.
U ovoj vrsti sortiranja 2D vektor je sortiran na osnovu br. stupca u silaznom redoslijedu. To se postiže prenošenjem trećeg argumenta u sort () kao pozivu na korisnika definiranu eksplicitnu funkciju.
// 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; }
Izlaz:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 3 4 5 1 2 6
Vremenska složenost: O (nlog (n))
Složena složenost: O (n*m)