Разговарали смо о неким случајевима сортирања 2Д вектора испод постављеног 1 и постављен 2.
Сортирање 2Д вектор у Ц ++ | Сет 1 (по ред и колони)
Сортирање 2Д вектор у Ц ++ | Сет 2 (у силазном редоследу по реду и колони)
У овом чланку се расправља више случајева
Као што је споменуто у једном од члана објављен овај сет, 2Д вектор такође може имати редове са различитим бројем ступаца. Ова некретнина је за разлику од 2Д низа у којем сви редови имају исти број ступаца.
инкапсулација у јава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< 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
Сложеност времена: О (н * м) н је број редова и м је број ступаца
СПЕЦЕР ЦОМЕКСНОБНОСТ: О (н * м)
Случај 5: Сортирање 2Д вектора на основу бр. стубова у низу у узлазном редоследу.
У овој врсти сортирања 2Д вектор је сортиран на основу не. колоне у узлазном редоследу. То се постиже пролазном трећем аргументом у сортирању () као позиву кориснику дефинисаној експлицитној функцији.
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
Сложеност времена: О (нлог (н))
СПЕЦЕР ЦОМЕКСНОБНОСТ: О (н * м)
Случај 6: Сортирање 2Д вектора на основу бр. стубова у низу у силазном редоследу.
У овој врсти сортирања 2Д вектор је сортиран на основу не. од колоне у силазном редоследу. То се постиже пролазном трећем аргументом у сортирању () као позиву кориснику дефинисаној експлицитној функцији.
// 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
Сложеност времена: О (нлог (н))
СПЕЦЕР ЦОМЕКСНОБНОСТ: О (н * м)