0Pricing
C++ Academy · Lesson

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 ascending

Custom 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; });   // descending

All lessons in this course

  1. Non-Modifying Algorithms find count all_of
  2. Modifying transform copy_if replace
  3. Sorting and Partitioning sort stable_partition
  4. Numeric Algorithms accumulate reduce transform_reduce
← Back to C++ Academy