Sorting and Partitioning sort stable_partition
Sort and partition containers with std::sort and std::stable_partition.
std::sort
The workhorse sorting algorithm. O(N log N) average. In place. Not guaranteed stable.
#include <algorithm>
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6};
std::sort(v.begin(), v.end());
// v is sorted ascendingCustom Comparator
Pass a comparator (lambda or functor) to sort by other criteria.
std::sort(v.begin(), v.end(),
[](int a, int b) { return a > b; }); // descendingAll lessons in this course
- Non-Modifying Algorithms find count all_of
- Modifying transform copy_if replace
- Sorting and Partitioning sort stable_partition
- Numeric Algorithms accumulate reduce transform_reduce