Sorting values is quite a standard task, and it can be done in various ways. Every computer science student who was tortured with having to learn a majority of existing sorting algorithms (together with their performance and stability trade-offs for exams) knows that.
Because this is a solved problem, programmers should not waste their time in solving it again, except if it is for learning purposes.
In this section, we are going to play with std::sort
and std::partial_sort
:
- First, we include all that's necessary and declare that we use the
std
namespace:
#include <iostream> #include <algorithm> #include <vector> #include <iterator> #include <random> using namespace std;
- We will print the state of a vector of integers multiple times, so let's abbreviate this task by writing a small procedure:
static void print(const vector<int> &v) { copy(begin(v)...