3. Common sorting algorithms
learning objective: master common sorting algorithms.
introduction to algorithm:
- Sort / / sort the elements in the container
- random_shuffle / / shuffle and randomly adjust the order of elements within the specified range
- Merge / / merge container elements and store them in another container
- reverse / / reverses the elements of the specified range
3.1 sort
Function Description: sort the elements in the container.
function prototype: sort(iterator beg, iterator end, _Pred);
/ / find the element by value, find the iterator that returns the specified position, and the iterator that returns the end position cannot be found
/ / beg start iterator
/ / end iterator
// _ Pred predicate
examples are as follows.
#include<iostream> using namespace std; #include<algorithm> #include<vector> #include<functional> //Common sorting algorithm: sort void myPrint(int val) { cout << val << " "; } void test01() { vector<int>v; v.push_back(10); v.push_back(30); v.push_back(50); v.push_back(20); v.push_back(40); //Ascending order using sort sort(v.begin(), v.end()); for_each(v.begin(), v.end(), myPrint); cout << endl; //Change to descending order sort(v.begin(), v.end(), greater<int>()); for_each(v.begin(), v.end(), myPrint); cout << endl; } int main() { test01(); system("pause"); return 0; }
10 20 30 40 50
50 40 30 20 10
summary: sort is one of the most commonly used algorithms in development and needs to be mastered.
3.2 random_shuffle
Function Description: shuffle and randomly adjust the order of elements within the specified range.
function prototype: random_shuffle(iterator beg, iterator end);
/ / randomly adjust the order of elements within the specified range
/ / beg start iterator
/ / end iterator
examples are as follows.
#include<iostream> using namespace std; #include<algorithm> #include<vector> #include<functional> #include<ctime> //Common sorting algorithm: random_shuffle void myPrint(int val) { cout << val << " "; } void test01() { srand((unsigned)time(NULL)); vector<int>v; for (int i = 0; i < 10; i++) { v.push_back(i); } //Using random_shuffle shuffle algorithm disrupts the order random_shuffle(v.begin(), v.end()); for_each(v.begin(), v.end(), myPrint); cout << endl; } int main() { test01(); system("pause"); return 0; }
7 8 6 5 3 2 4 0 9 1
summary: random_shuffle shuffle algorithm is more practical. Remember to add random number seeds when using it.
3.3 merge
Function Description: two container elements are merged and stored in another container.
function prototype: merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
/ / merge container elements and store them in another container
/ / Note: the two containers must be in order
/ / beg1 container 1 starts iterator
/ / end1 container 1 end iterator
/ / beg2 container 2 starts iterator
/ / end2 container 2 end iterator
/ / dest target container starts iterator
examples are as follows.
#include<iostream> using namespace std; #include<algorithm> #include<vector> #include<functional> #include<ctime> //Common sorting algorithm: merge void myPrint(int val) { cout << val << " "; } void test01() { vector<int>v1; vector<int>v2; for (int i = 0; i < 10; i++) { v1.push_back(i); v2.push_back(i + 1); } //Target container vector<int>vTarget; vTarget.resize(v1.size() + v2.size()); //The target container needs to open up space in advance //merge two containers v1 and v2 and store them in another container vTarget merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin()); for_each(vTarget.begin(), vTarget.end(), myPrint); cout << endl; } int main() { test01(); system("pause"); return 0; }
0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
summary: 1 The two containers merged by merge must be an ordered sequence;
2. The target container needs to open up space in advance.
5.3.4 reverse
Function Description: invert the elements in the container.
function prototype: reverse(iterator beg, iterator end);
/ / reverses the elements of the specified range
/ / beg start iterator
/ / end iterator
examples are as follows.
#include<iostream> using namespace std; #include<algorithm> #include<vector> #include<functional> //Common sorting algorithm: reverse void myPrint(int val) { cout << val << " "; } void test01() { vector<int>v; v.push_back(10); v.push_back(30); v.push_back(50); v.push_back(20); v.push_back(40); cout << "Before reversing:" << endl; for_each(v.begin(), v.end(), myPrint); cout << endl; //Use reverse to reverse the elements in the container reverse(v.begin(), v.end()); cout << "After reversal:" << endl; for_each(v.begin(), v.end(), myPrint); cout << endl; } int main() { test01(); system("pause"); return 0; }
Before reversing:
10 30 50 20 40
After reversal:
40 20 50 30 10
summary: reverse reverses the elements in the interval, which may be involved in the interview question.