We can improve on the approach above, using std::map which holds data in ascending order.
Creating a std::map object to help record some values.
std::map<int, int> all_values;
Since we do not know the size of the array, we can call the range-based for loop to iterate the array
and insert each element of the array into the map object, incrementing the count on each iteration.
for (int elem : arr)
{
++all_values[elem];
}
We will iterate over the elements in the map and insert into our array as many times as there may be in the second value of each pair.
keeping track of the current index in the array.
int index {-1};
for (auto& [num, count] : all_values)
{
for (int i{}; i < count; ++i)
{
arr[++index] = num;
}
}
Currently, our array holds all elements in ascending order.