Files
2025-10-25 03:02:53 +03:00

82 lines
5.0 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[sort]
# 26 Algorithms library [[algorithms]](./#algorithms)
## 26.8 Sorting and related operations [[alg.sorting]](alg.sorting#sort)
### 26.8.2 Sorting [[alg.sort]](alg.sort#sort)
#### 26.8.2.1 sort [sort]
[🔗](#lib:sort)
`template<class RandomAccessIterator>
constexpr void sort(RandomAccessIterator first, RandomAccessIterator last);
template<class ExecutionPolicy, class RandomAccessIterator>
void sort(ExecutionPolicy&& exec,
RandomAccessIterator first, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare>
constexpr void sort(RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
template<class ExecutionPolicy, class RandomAccessIterator, class Compare>
void sort(ExecutionPolicy&& exec,
RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
template<[random_access_iterator](iterator.concept.random.access#concept:random_access_iterator "24.3.4.13Concept random_­access_­iterator[iterator.concept.random.access]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7Concept sentinel_­for[iterator.concept.sentinel]")<I> S, class Comp = ranges::less,
class Proj = identity>
requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8Concept sortable[alg.req.sortable]")<I, Comp, Proj>
constexpr I
ranges::sort(I first, S last, Comp comp = {}, Proj proj = {});
template<[random_access_range](range.refinements#concept:random_access_range "25.4.6Other range refinements[range.refinements]") R, class Comp = ranges::less, class Proj = identity>
requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8Concept sortable[alg.req.sortable]")<iterator_t<R>, Comp, Proj>
constexpr borrowed_iterator_t<R>
ranges::sort(R&& r, Comp comp = {}, Proj proj = {});
template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1Preamble[algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random_access_iterator "24.3.4.13Concept random_­access_­iterator[iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8Concept sized_­sentinel_­for[iterator.concept.sizedsentinel]")<I> S,
class Comp = ranges::less, class Proj = identity>
requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8Concept sortable[alg.req.sortable]")<I, Comp, Proj>
I ranges::sort(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {});
template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1Preamble[algorithms.parallel.defns]") Ep, [sized-random-access-range](range.refinements#concept:sized-random-access-range "25.4.6Other range refinements[range.refinements]") R, class Comp = ranges::less,
class Proj = identity>
requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8Concept sortable[alg.req.sortable]")<iterator_t<R>, Comp, Proj>
borrowed_iterator_t<R> ranges::sort(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {});
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8795)
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8800)
*Preconditions*: For the overloads in namespace std,RandomAccessIterator meets
the [*Cpp17ValueSwappable*](swappable.requirements#:Cpp17ValueSwappable "16.4.4.3Swappable requirements[swappable.requirements]") requirements ([[swappable.requirements]](swappable.requirements "16.4.4.3Swappable requirements")) and
the type of *first meets
the [*Cpp17MoveConstructible*](utility.arg.requirements#:Cpp17MoveConstructible "16.4.4.2Template argument requirements[utility.arg.requirements]") (Table [31](utility.arg.requirements#tab:cpp17.moveconstructible "Table 31: Cpp17MoveConstructible requirements")) and[*Cpp17MoveAssignable*](utility.arg.requirements#:Cpp17MoveAssignable "16.4.4.2Template argument requirements[utility.arg.requirements]") (Table [33](utility.arg.requirements#tab:cpp17.moveassignable "Table 33: Cpp17MoveAssignable requirements")) requirements[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8809)
*Effects*: Sorts the elements in the range [first, last)
with respect to comp and proj[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8814)
*Returns*: last for the overloads in namespace ranges[.](#4.sentence-1)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L8818)
*Complexity*: Let N be last - first[.](#5.sentence-1)
O(NlogN) comparisons and projections[.](#5.sentence-2)