36 KiB
[alg.sort]
26 Algorithms library [algorithms]
26.8 Sorting and related operations [alg.sorting]
26.8.2 Sorting [alg.sort]
26.8.2.1 sort [sort]
`template 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 I, sentinel_for S, class Comp = ranges::less, class Proj = identity> requires sortable<I, Comp, Proj> constexpr I ranges::sort(I first, S last, Comp comp = {}, Proj proj = {}); template<random_access_range R, class Comp = ranges::less, class Proj = identity> requires sortable<iterator_t, Comp, Proj> constexpr borrowed_iterator_t ranges::sort(R&& r, Comp comp = {}, Proj proj = {});
template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Comp = ranges::less, class Proj = identity> requires sortable<I, Comp, Proj> I ranges::sort(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, class Comp = ranges::less, class Proj = identity> requires sortable<iterator_t, Comp, Proj> borrowed_iterator_t ranges::sort(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {}); `
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Preconditions: For the overloads in namespace std,RandomAccessIterator meets the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first meets the Cpp17MoveConstructible (Table 31) andCpp17MoveAssignable (Table 33) requirements.
Effects: Sorts the elements in the range [first, last) with respect to comp and proj.
Returns: last for the overloads in namespace ranges.
Complexity: Let N be last - first.
O(NlogN) comparisons and projections.
26.8.2.2 stable_sort [stable.sort]
`template constexpr void stable_sort(RandomAccessIterator first, RandomAccessIterator last); template<class ExecutionPolicy, class RandomAccessIterator> void stable_sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare> constexpr void stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); template<class ExecutionPolicy, class RandomAccessIterator, class Compare> void stable_sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last, Compare comp);
template<random_access_iterator I, sentinel_for S, class Comp = ranges::less, class Proj = identity> requires sortable<I, Comp, Proj> constexpr I ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {}); template<random_access_range R, class Comp = ranges::less, class Proj = identity> requires sortable<iterator_t, Comp, Proj> constexpr borrowed_iterator_t ranges::stable_sort(R&& r, Comp comp = {}, Proj proj = {});
template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Comp = ranges::less, class Proj = identity> requires sortable<I, Comp, Proj> I ranges::stable_sort(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, class Comp = ranges::less, class Proj = identity> requires sortable<iterator_t, Comp, Proj> borrowed_iterator_t ranges::stable_sort(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {}); `
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Preconditions: For the overloads in namespace std,RandomAccessIterator meets the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first meets the Cpp17MoveConstructible (Table 31) andCpp17MoveAssignable (Table 33) requirements.
Effects: Sorts the elements in the range [first, last) with respect to comp and proj.
Returns: last for the overloads in namespace ranges.
Complexity: Let N be last - first.
If enough extra memory is available, Nlog(N) comparisons.
Otherwise, at most Nlog2(N) comparisons.
In either case, twice as many projections as the number of comparisons.
Remarks: Stable ([algorithm.stable]).
26.8.2.3 partial_sort [partial.sort]
`template constexpr void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); template<class ExecutionPolicy, class RandomAccessIterator> void partial_sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
template<class RandomAccessIterator, class Compare> constexpr void partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); template<class ExecutionPolicy, class RandomAccessIterator, class Compare> void partial_sort(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
template<random_access_iterator I, sentinel_for S, class Comp = ranges::less, class Proj = identity> requires sortable<I, Comp, Proj> constexpr I ranges::partial_sort(I first, I middle, S last, Comp comp = {}, Proj proj = {}); template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Comp = ranges::less, class Proj = identity> requires sortable<I, Comp, Proj> I ranges::partial_sort(Ep&& exec, I first, I middle, S last, Comp comp = {}, Proj proj = {}); `
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Preconditions: [first, middle) and [middle, last) are valid ranges.
For the overloads in namespace std,RandomAccessIterator meets the Cpp17ValueSwappable requirements ([swappable.requirements]) and the type of *first meets the Cpp17MoveConstructible (Table 31) andCpp17MoveAssignable (Table 33) requirements.
Effects: Places the first middle - first elements from the range [first, last) as sorted with respect to comp and proj into the range [first, middle).
The rest of the elements in the range [middle, last) are placed in an unspecified order.
Returns: last for the overload in namespace ranges.
Complexity: Approximately (last - first) * log(middle - first) comparisons, and twice as many projections.
template<[random_access_range](range.refinements#concept:random_access_range "25.4.6 Other range refinements [range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<iterator_t<R>, Comp, Proj> constexpr borrowed_iterator_t<R> ranges::partial_sort(R&& r, iterator_t<R> middle, Comp comp = {}, Proj proj = {});
Effects: Equivalent to:return ranges::partial_sort(ranges::begin(r), middle, ranges::end(r), comp, proj);
template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [sized-random-access-range](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<iterator_t<R>, Comp, Proj> borrowed_iterator_t<R> ranges::partial_sort(Ep&& exec, R&& r, iterator_t<R> middle, Comp comp = {}, Proj proj = {});
Effects: Equivalent to:return ranges::partial_sort(std::forward(exec), ranges::begin(r), middle, ranges::end(r), comp, proj);
26.8.2.4 partial_sort_copy [partial.sort.copy]
`template<class InputIterator, class RandomAccessIterator> constexpr RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last); template<class ExecutionPolicy, class ForwardIterator, class RandomAccessIterator> RandomAccessIterator partial_sort_copy(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last);
template<class InputIterator, class RandomAccessIterator, class Compare> constexpr RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); template<class ExecutionPolicy, class ForwardIterator, class RandomAccessIterator, class Compare> RandomAccessIterator partial_sort_copy(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
template<input_iterator I1, sentinel_for S1, random_access_iterator I2, sentinel_for S2, class Comp = ranges::less, class Proj1 = identity, class Proj2 = identity> requires indirectly_copyable<I1, I2> && sortable<I2, Comp, Proj2> && indirect_strict_weak_order<Comp, projected<I1, Proj1>, projected<I2, Proj2>> constexpr ranges::partial_sort_copy_result<I1, I2> ranges::partial_sort_copy(I1 first, S1 last, I2 result_first, S2 result_last, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<input_range R1, random_access_range R2, class Comp = ranges::less, class Proj1 = identity, class Proj2 = identity> requires indirectly_copyable<iterator_t, iterator_t> && sortable<iterator_t, Comp, Proj2> && indirect_strict_weak_order<Comp, projected<iterator_t, Proj1>, projected<iterator_t, Proj2>> constexpr ranges::partial_sort_copy_result<borrowed_iterator_t, borrowed_iterator_t> ranges::partial_sort_copy(R1&& r, R2&& result_r, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});
template<execution-policy Ep, random_access_iterator I1, sized_sentinel_for S1, random_access_iterator I2, sized_sentinel_for S2, class Comp = ranges::less, class Proj1 = identity, class Proj2 = identity> requires indirectly_copyable<I1, I2> && sortable<I2, Comp, Proj2> && indirect_strict_weak_order<Comp, projected<I1, Proj1>, projected<I2, Proj2>> ranges::partial_sort_copy_result<I1, I2> ranges::partial_sort_copy(Ep&& exec, I1 first, S1 last, I2 result_first, S2 result_last, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); template<execution-policy Ep, sized-random-access-range R1, sized-random-access-range R2, class Comp = ranges::less, class Proj1 = identity, class Proj2 = identity> requires indirectly_copyable<iterator_t, iterator_t> && sortable<iterator_t, Comp, Proj2> && indirect_strict_weak_order<Comp, projected<iterator_t, Proj1>, projected<iterator_t, Proj2>> ranges::partial_sort_copy_result<borrowed_iterator_t, borrowed_iterator_t> ranges::partial_sort_copy(Ep&& exec, R1&& r, R2&& result_r, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); `
Let N be min(last - first, result_last - result_first).
Let comp be less{}, andproj1 and proj2 be identity{} for the overloads with no parameters by those names.
Mandates: For the overloads in namespace std, the expression *first is writable ([iterator.requirements.general]) to result_first.
Preconditions: For the overloads in namespace std,RandomAccessIterator meets the Cpp17ValueSwappable requirements ([swappable.requirements]), the type of *result_first meets the Cpp17MoveConstructible (Table 31) andCpp17MoveAssignable (Table 33) requirements.
For iterators a1 and b1 in [first, last), and iterators x2 and y2 in [result_first, result_last), after evaluating the assignment *y2 = *b1, let E be the value ofbool(invoke(comp, invoke(proj1, *a1), invoke(proj2, *y2))).
Then, after evaluating the assignment *x2 = *a1, E is equal tobool(invoke(comp, invoke(proj2, *x2), invoke(proj2, *y2))).
[Note 1:
Writing a value from the input range into the output range does not affect how it is ordered by comp and proj1 or proj2.
â end note]
Effects: Places the first N elements as sorted with respect to comp and proj2 into the range [result_first, result_first + N).
Returns:
-
result_first + N for the overloads in namespace std.
-
{last, result_first + N} for the overloads in namespace ranges.
Complexity: Approximately (last - first) * log N comparisons, and twice as many projections.
26.8.2.5 is_sorted [is.sorted]
template<class ForwardIterator> constexpr bool is_sorted(ForwardIterator first, ForwardIterator last);
Effects: Equivalent to: return is_sorted_until(first, last) == last;
template<class ExecutionPolicy, class ForwardIterator> bool is_sorted(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last);
Effects: Equivalent to:return is_sorted_until(std::forward(exec), first, last) == last;
template<class ForwardIterator, class Compare> constexpr bool is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
Effects: Equivalent to: return is_sorted_until(first, last, comp) == last;
template<class ExecutionPolicy, class ForwardIterator, class Compare> bool is_sorted(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Compare comp);
Effects: Equivalent to:return is_sorted_until(std::forward(exec), first, last, comp) == last;
template<[forward_iterator](iterator.concept.forward#concept:forward_iterator "24.3.4.11 Concept forward_iterator [iterator.concept.forward]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel_for "24.3.4.7 Concept sentinel_for [iterator.concept.sentinel]")<I> S, class Proj = identity, [indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect_strict_weak_order "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> constexpr bool ranges::is_sorted(I first, S last, Comp comp = {}, Proj proj = {}); template<[forward_range](range.refinements#concept:forward_range "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity, [indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect_strict_weak_order "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> constexpr bool ranges::is_sorted(R&& r, Comp comp = {}, Proj proj = {});
Effects: Equivalent to:return ranges::is_sorted_until(first, last, comp, proj) == last;
template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random_access_iterator "24.3.4.13 Concept random_access_iterator [iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized_sentinel_for "24.3.4.8 Concept sized_sentinel_for [iterator.concept.sizedsentinel]")<I> S, class Proj = identity, [indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect_strict_weak_order "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> bool ranges::is_sorted(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {}); template<[execution-policy](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [sized-random-access-range](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity, [indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect_strict_weak_order "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> bool ranges::is_sorted(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {});
Effects: Equivalent to:return ranges::is_sorted_until(std::forward(exec), first, last, comp, proj) == last;
`template constexpr ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last); template<class ExecutionPolicy, class ForwardIterator> ForwardIterator is_sorted_until(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Compare> constexpr ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp); template<class ExecutionPolicy, class ForwardIterator, class Compare> ForwardIterator is_sorted_until(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, Compare comp);
template<forward_iterator I, sentinel_for S, class Proj = identity, indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less> constexpr I ranges::is_sorted_until(I first, S last, Comp comp = {}, Proj proj = {}); template<forward_range R, class Proj = identity, indirect_strict_weak_order<projected<iterator_t, Proj>> Comp = ranges::less> constexpr borrowed_iterator_t ranges::is_sorted_until(R&& r, Comp comp = {}, Proj proj = {});
template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S, class Proj = identity, indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less> I ranges::is_sorted_until(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {}); template<execution-policy Ep, sized-random-access-range R, class Proj = identity, indirect_strict_weak_order<projected<iterator_t, Proj>> Comp = ranges::less> borrowed_iterator_t ranges::is_sorted_until(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {}); `
Let comp be less{} and proj be identity{} for the overloads with no parameters by those names.
Returns: The last iterator i in [first, last] for which the range [first, i) is sorted with respect to comp and proj.
Complexity: Linear.