On this page
  1. std::sort / std::ranges::sort
  2. Overview
  3. Notes & Pitfalls
  4. Example

std::sort / std::ranges::sort

Prev: algorithm_and_ranges | Up: algorithm_and_ranges | Next: std::find_if
Rel: std::vector, Lambdas
Ref: cppreference — std::ranges::sort
Tags: #public #prog #cpp #stdlib #algorithms #tier/core


Overview

std::sort / std::ranges::sort (<algorithm>) performs an O(n log n) worst-case in-place sort (typically introsort: quicksort + heapsort fallback + insertion sort) on any random-access range.

Notes & Pitfalls

Example

#include <algorithm>
#include <cassert>
#include <vector>

int main() {
    std::vector<int> v{4, 1, 3, 2};
    std::sort(v.begin(), v.end());
    assert((v == std::vector<int>{1, 2, 3, 4}));
}

Linked from

Related notes