From Jason Turner

[alg.sorting]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpzefr_wpc/{from.md → to.md} +562 -144
tmp/tmpzefr_wpc/{from.md → to.md} RENAMED
@@ -14,37 +14,43 @@ ordering relation. It is assumed that `comp` will not apply any
14
  non-constant function through the dereferenced iterator.
15
 
16
  For all algorithms that take `Compare`, there is a version that uses
17
  `operator<` instead. That is, `comp(*i, *j) != false` defaults to
18
  `*i < *j != false`. For algorithms other than those described in 
19
- [[alg.binary.search]] to work correctly, `comp` has to induce a strict
20
- weak ordering on the values.
21
 
22
  The term *strict* refers to the requirement of an irreflexive relation
23
  (`!comp(x, x)` for all `x`), and the term *weak* to requirements that
24
  are not as strong as those for a total ordering, but stronger than those
25
  for a partial ordering. If we define `equiv(a, b)` as
26
  `!comp(a, b) && !comp(b, a)`, then the requirements are that `comp` and
27
  `equiv` both be transitive relations:
28
 
29
  - `comp(a, b) && comp(b, c)` implies `comp(a, c)`
30
- - `equiv(a, b) && equiv(b, c)`
31
- implies `equiv(a, c)` Under these conditions, it can be shown that
 
 
 
 
32
  - `equiv` is an equivalence relation
33
  - `comp` induces a well-defined relation on the equivalence classes
34
  determined by `equiv`
35
  - The induced relation is a strict total ordering.
36
 
 
 
37
  A sequence is *sorted with respect to a comparator* `comp` if for every
38
  iterator `i` pointing to the sequence and every non-negative integer `n`
39
  such that `i + n` is a valid iterator pointing to an element of the
40
  sequence, `comp(*(i + n), *i) == false`.
41
 
42
  A sequence \[`start`, `finish`) is *partitioned with respect to an
43
  expression* `f(e)` if there exists an integer `n` such that for all
44
- `0 <= i < distance(start, finish)`, `f(*(start + i))` is true if and
45
- only if `i < n`.
46
 
47
  In the descriptions of the functions that deal with ordering
48
  relationships we frequently use a notion of equivalence to describe
49
  concepts such as stability. The equivalence to which we refer is not
50
  necessarily an `operator==`, but an equivalence relation induced by the
@@ -56,111 +62,150 @@ equivalent if and only if `!(a < b) && !(b < a)`.
56
  #### `sort` <a id="sort">[[sort]]</a>
57
 
58
  ``` cpp
59
  template<class RandomAccessIterator>
60
  void sort(RandomAccessIterator first, RandomAccessIterator last);
 
 
 
61
 
62
  template<class RandomAccessIterator, class Compare>
63
  void sort(RandomAccessIterator first, RandomAccessIterator last,
64
  Compare comp);
 
 
 
 
65
  ```
66
 
67
- *Effects:* Sorts the elements in the range \[`first`, `last`).
68
-
69
  *Requires:* `RandomAccessIterator` shall satisfy the requirements of
70
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
71
  shall satisfy the requirements of `MoveConstructible`
72
- (Table  [[moveconstructible]]) and of `MoveAssignable`
73
- (Table  [[moveassignable]]).
74
 
75
- *Complexity:* 𝑂(Nlog(N)) (where N` == last - first`) comparisons.
 
 
76
 
77
  #### `stable_sort` <a id="stable.sort">[[stable.sort]]</a>
78
 
79
  ``` cpp
80
  template<class RandomAccessIterator>
81
  void stable_sort(RandomAccessIterator first, RandomAccessIterator last);
 
 
 
82
 
83
  template<class RandomAccessIterator, class Compare>
84
  void stable_sort(RandomAccessIterator first, RandomAccessIterator last,
85
  Compare comp);
 
 
 
 
86
  ```
87
 
88
- *Effects:* Sorts the elements in the range \[`first`, `last`).
89
-
90
  *Requires:* `RandomAccessIterator` shall satisfy the requirements of
91
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
92
  shall satisfy the requirements of `MoveConstructible`
93
- (Table  [[moveconstructible]]) and of `MoveAssignable`
94
- (Table  [[moveassignable]]).
95
 
96
- *Complexity:* It does at most N log²(N) (where N` == last - first`)
97
- comparisons; if enough extra memory is available, it is N log(N).
 
 
98
 
99
  *Remarks:* Stable ([[algorithm.stable]]).
100
 
101
  #### `partial_sort` <a id="partial.sort">[[partial.sort]]</a>
102
 
103
  ``` cpp
104
  template<class RandomAccessIterator>
105
  void partial_sort(RandomAccessIterator first,
106
  RandomAccessIterator middle,
107
  RandomAccessIterator last);
 
 
 
 
 
108
 
109
  template<class RandomAccessIterator, class Compare>
110
  void partial_sort(RandomAccessIterator first,
111
  RandomAccessIterator middle,
112
  RandomAccessIterator last,
113
  Compare comp);
 
 
 
 
 
 
114
  ```
115
 
 
 
 
 
 
 
116
  *Effects:* Places the first `middle - first` sorted elements from the
117
  range \[`first`, `last`) into the range \[`first`, `middle`). The rest
118
  of the elements in the range \[`middle`, `last`) are placed in an
119
  unspecified order.
120
 
121
- *Requires:* `RandomAccessIterator` shall satisfy the requirements of
122
- `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
123
- shall satisfy the requirements of `MoveConstructible`
124
- (Table  [[moveconstructible]]) and of `MoveAssignable`
125
- (Table  [[moveassignable]]).
126
-
127
- *Complexity:* It takes approximately
128
- `(last - first) * log(middle - first)` comparisons.
129
 
130
  #### `partial_sort_copy` <a id="partial.sort.copy">[[partial.sort.copy]]</a>
131
 
132
  ``` cpp
133
  template<class InputIterator, class RandomAccessIterator>
134
  RandomAccessIterator
135
  partial_sort_copy(InputIterator first, InputIterator last,
136
  RandomAccessIterator result_first,
137
  RandomAccessIterator result_last);
 
 
 
 
 
 
138
 
139
  template<class InputIterator, class RandomAccessIterator,
140
  class Compare>
141
  RandomAccessIterator
142
  partial_sort_copy(InputIterator first, InputIterator last,
143
  RandomAccessIterator result_first,
144
  RandomAccessIterator result_last,
145
  Compare comp);
 
 
 
 
 
 
 
 
146
  ```
147
 
 
 
 
 
 
 
148
  *Effects:* Places the first
149
  `min(last - first, result_last - result_first)` sorted elements into the
150
  range \[`result_first`,
151
  `result_first + min(last - first, result_last - result_first)`).
152
 
153
  *Returns:* The smaller of: `result_last` or
154
  `result_first + (last - first)`.
155
 
156
- *Requires:* `RandomAccessIterator` shall satisfy the requirements of
157
- `ValueSwappable` ([[swappable.requirements]]). The type of
158
- `*result_first` shall satisfy the requirements of `MoveConstructible`
159
- (Table  [[moveconstructible]]) and of `MoveAssignable`
160
- (Table  [[moveassignable]]).
161
-
162
  *Complexity:* Approximately
163
  `(last - first) * log(min(last - first, result_last - result_first))`
164
  comparisons.
165
 
166
  #### `is_sorted` <a id="is.sorted">[[is.sorted]]</a>
@@ -170,57 +215,97 @@ template<class ForwardIterator>
170
  bool is_sorted(ForwardIterator first, ForwardIterator last);
171
  ```
172
 
173
  *Returns:* `is_sorted_until(first, last) == last`
174
 
 
 
 
 
 
 
 
 
 
175
  ``` cpp
176
  template<class ForwardIterator, class Compare>
177
  bool is_sorted(ForwardIterator first, ForwardIterator last,
178
  Compare comp);
179
  ```
180
 
181
  *Returns:* `is_sorted_until(first, last, comp) == last`
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  ``` cpp
184
  template<class ForwardIterator>
185
  ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last);
 
 
 
 
186
  template<class ForwardIterator, class Compare>
187
  ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last,
188
  Compare comp);
 
 
 
 
189
  ```
190
 
191
- *Returns:* If `distance(first, last) < 2`, returns `last`. Otherwise,
192
- returns the last iterator `i` in \[`first`, `last`\] for which the range
193
  \[`first`, `i`) is sorted.
194
 
195
  *Complexity:* Linear.
196
 
197
  ### Nth element <a id="alg.nth.element">[[alg.nth.element]]</a>
198
 
199
  ``` cpp
200
  template<class RandomAccessIterator>
201
  void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
202
  RandomAccessIterator last);
 
 
 
 
203
 
204
  template<class RandomAccessIterator, class Compare>
205
  void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
206
  RandomAccessIterator last, Compare comp);
 
 
 
 
207
  ```
208
 
209
- After `nth_element` the element in the position pointed to by `nth` is
210
- the element that would be in that position if the whole range were
211
- sorted, unless `nth == last`. Also for every iterator `i` in the range
212
- \[`first`, `nth`) and every iterator `j` in the range \[`nth`, `last`)
213
- it holds that: `!(*j < *i)` or `comp(*j, *i) == false`.
214
-
215
  *Requires:* `RandomAccessIterator` shall satisfy the requirements of
216
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
217
  shall satisfy the requirements of `MoveConstructible`
218
- (Table  [[moveconstructible]]) and of `MoveAssignable`
219
- (Table  [[moveassignable]]).
220
 
221
- *Complexity:* Linear on average.
 
 
 
 
 
 
 
 
222
 
223
  ### Binary search <a id="alg.binary.search">[[alg.binary.search]]</a>
224
 
225
  All of the algorithms in this section are versions of binary search and
226
  assume that the sequence being searched is partitioned with respect to
@@ -340,77 +425,250 @@ implies `!comp(value, e)`.
340
  `!(*i < value) && !(value < *i)` or
341
  `comp(*i, value) == false && comp(value, *i) == false`.
342
 
343
  *Complexity:* At most log₂(`last - first`) + 𝑂(1) comparisons.
344
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
  ### Merge <a id="alg.merge">[[alg.merge]]</a>
346
 
347
  ``` cpp
348
  template<class InputIterator1, class InputIterator2,
349
  class OutputIterator>
350
  OutputIterator
351
  merge(InputIterator1 first1, InputIterator1 last1,
352
  InputIterator2 first2, InputIterator2 last2,
353
  OutputIterator result);
 
 
 
 
 
 
 
354
 
355
  template<class InputIterator1, class InputIterator2,
356
  class OutputIterator, class Compare>
357
  OutputIterator
358
  merge(InputIterator1 first1, InputIterator1 last1,
359
  InputIterator2 first2, InputIterator2 last2,
360
  OutputIterator result, Compare comp);
 
 
 
 
 
 
 
361
  ```
362
 
 
 
 
 
363
  *Effects:*  Copies all the elements of the two ranges \[`first1`,
364
  `last1`) and \[`first2`, `last2`) into the range \[`result`,
365
  `result_last`), where `result_last` is
366
  `result + (last1 - first1) + (last2 - first2)`, such that the resulting
367
  range satisfies `is_sorted(result, result_last)` or
368
  `is_sorted(result, result_last, comp)`, respectively.
369
 
370
- *Requires:* The ranges \[`first1`, `last1`) and \[`first2`, `last2`)
371
- shall be sorted with respect to `operator<` or `comp`. The resulting
372
- range shall not overlap with either of the original ranges.
373
-
374
  *Returns:* `result + (last1 - first1) + (last2 - first2)`.
375
 
376
- *Complexity:* At most `(last1 - first1) + (last2 - first2) - 1`
 
 
377
  comparisons.
 
378
 
379
  *Remarks:* Stable ([[algorithm.stable]]).
380
 
381
  ``` cpp
382
  template<class BidirectionalIterator>
383
  void inplace_merge(BidirectionalIterator first,
384
  BidirectionalIterator middle,
385
  BidirectionalIterator last);
 
 
 
 
 
386
 
387
  template<class BidirectionalIterator, class Compare>
388
  void inplace_merge(BidirectionalIterator first,
389
  BidirectionalIterator middle,
390
  BidirectionalIterator last, Compare comp);
 
 
 
 
 
391
  ```
392
 
 
 
 
 
 
 
 
 
393
  *Effects:* Merges two sorted consecutive ranges \[`first`, `middle`) and
394
  \[`middle`, `last`), putting the result of the merge into the range
395
  \[`first`, `last`). The resulting range will be in non-decreasing order;
396
  that is, for every iterator `i` in \[`first`, `last`) other than
397
  `first`, the condition `*i < *(i - 1)` or, respectively,
398
- `comp(*i, *(i - 1))` will be false.
399
 
400
- *Requires:* The ranges \[`first`, `middle`) and \[`middle`, `last`)
401
- shall be sorted with respect to `operator<` or `comp`.
402
- `BidirectionalIterator` shall satisfy the requirements of
403
- `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
404
- shall satisfy the requirements of `MoveConstructible`
405
- (Table  [[moveconstructible]]) and of `MoveAssignable`
406
- (Table  [[moveassignable]]).
407
 
408
- *Complexity:* When enough additional memory is available,
409
- `(last - first) - 1` comparisons. If no additional memory is available,
410
- an algorithm with complexity N log(N) (where `N` is equal to
411
- `last - first`) may be used.
 
412
 
413
  *Remarks:* Stable ([[algorithm.stable]]).
414
 
415
  ### Set operations on sorted structures <a id="alg.set.operations">[[alg.set.operations]]</a>
416
 
@@ -425,15 +683,24 @@ to contain the maximum number of occurrences of every element,
425
 
426
  ``` cpp
427
  template<class InputIterator1, class InputIterator2>
428
  bool includes(InputIterator1 first1, InputIterator1 last1,
429
  InputIterator2 first2, InputIterator2 last2);
 
 
 
 
430
 
431
  template<class InputIterator1, class InputIterator2, class Compare>
432
  bool includes(InputIterator1 first1, InputIterator1 last1,
433
  InputIterator2 first2, InputIterator2 last2,
434
  Compare comp);
 
 
 
 
 
435
  ```
436
 
437
  *Returns:* `true` if \[`first2`, `last2`) is empty or if every element
438
  in the range \[`first2`, `last2`) is contained in the range \[`first1`,
439
  `last1`). Returns `false` otherwise.
@@ -448,26 +715,40 @@ template<class InputIterator1, class InputIterator2,
448
  class OutputIterator>
449
  OutputIterator
450
  set_union(InputIterator1 first1, InputIterator1 last1,
451
  InputIterator2 first2, InputIterator2 last2,
452
  OutputIterator result);
 
 
 
 
 
 
 
453
 
454
  template<class InputIterator1, class InputIterator2,
455
  class OutputIterator, class Compare>
456
  OutputIterator
457
  set_union(InputIterator1 first1, InputIterator1 last1,
458
  InputIterator2 first2, InputIterator2 last2,
459
  OutputIterator result, Compare comp);
 
 
 
 
 
 
 
460
  ```
461
 
 
 
 
462
  *Effects:* Constructs a sorted union of the elements from the two
463
  ranges; that is, the set of elements that are present in one or both of
464
  the ranges.
465
 
466
- *Requires:* The resulting range shall not overlap with either of the
467
- original ranges.
468
-
469
  *Returns:* The end of the constructed range.
470
 
471
  *Complexity:* At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
472
  comparisons.
473
 
@@ -485,26 +766,40 @@ template<class InputIterator1, class InputIterator2,
485
  class OutputIterator>
486
  OutputIterator
487
  set_intersection(InputIterator1 first1, InputIterator1 last1,
488
  InputIterator2 first2, InputIterator2 last2,
489
  OutputIterator result);
 
 
 
 
 
 
 
490
 
491
  template<class InputIterator1, class InputIterator2,
492
  class OutputIterator, class Compare>
493
  OutputIterator
494
  set_intersection(InputIterator1 first1, InputIterator1 last1,
495
  InputIterator2 first2, InputIterator2 last2,
496
  OutputIterator result, Compare comp);
 
 
 
 
 
 
 
497
  ```
498
 
 
 
 
499
  *Effects:* Constructs a sorted intersection of the elements from the two
500
  ranges; that is, the set of elements that are present in both of the
501
  ranges.
502
 
503
- *Requires:* The resulting range shall not overlap with either of the
504
- original ranges.
505
-
506
  *Returns:* The end of the constructed range.
507
 
508
  *Complexity:* At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
509
  comparisons.
510
 
@@ -520,26 +815,40 @@ template<class InputIterator1, class InputIterator2,
520
  class OutputIterator>
521
  OutputIterator
522
  set_difference(InputIterator1 first1, InputIterator1 last1,
523
  InputIterator2 first2, InputIterator2 last2,
524
  OutputIterator result);
 
 
 
 
 
 
 
525
 
526
  template<class InputIterator1, class InputIterator2,
527
  class OutputIterator, class Compare>
528
  OutputIterator
529
  set_difference(InputIterator1 first1, InputIterator1 last1,
530
  InputIterator2 first2, InputIterator2 last2,
531
  OutputIterator result, Compare comp);
 
 
 
 
 
 
 
532
  ```
533
 
 
 
 
534
  *Effects:* Copies the elements of the range \[`first1`, `last1`) which
535
  are not present in the range \[`first2`, `last2`) to the range beginning
536
  at `result`. The elements in the constructed range are sorted.
537
 
538
- *Requires:* The resulting range shall not overlap with either of the
539
- original ranges.
540
-
541
  *Returns:* The end of the constructed range.
542
 
543
  *Complexity:* At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
544
  comparisons.
545
 
@@ -555,28 +864,42 @@ template<class InputIterator1, class InputIterator2,
555
  class OutputIterator>
556
  OutputIterator
557
  set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
558
  InputIterator2 first2, InputIterator2 last2,
559
  OutputIterator result);
 
 
 
 
 
 
 
560
 
561
  template<class InputIterator1, class InputIterator2,
562
  class OutputIterator, class Compare>
563
  OutputIterator
564
  set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
565
  InputIterator2 first2, InputIterator2 last2,
566
  OutputIterator result, Compare comp);
 
 
 
 
 
 
 
567
  ```
568
 
 
 
 
569
  *Effects:* Copies the elements of the range \[`first1`, `last1`) that
570
  are not present in the range \[`first2`, `last2`), and the elements of
571
  the range \[`first2`, `last2`) that are not present in the range
572
  \[`first1`, `last1`) to the range beginning at `result`. The elements in
573
  the constructed range are sorted.
574
 
575
- *Requires:* The resulting range shall not overlap with either of the
576
- original ranges.
577
-
578
  *Returns:* The end of the constructed range.
579
 
580
  *Complexity:* At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
581
  comparisons.
582
 
@@ -588,24 +911,17 @@ copied to the output range: the last m - n of these elements from
588
  \[`first2`, `last2`) if m < n.
589
 
590
  ### Heap operations <a id="alg.heap.operations">[[alg.heap.operations]]</a>
591
 
592
  A *heap* is a particular organization of elements in a range between two
593
- random access iterators \[`a`, `b`). Its two key properties are:
594
 
595
- - **(1)} There is no element greater than
596
- \texttt{\*a}
597
- in the range and**
598
-
599
- - **`(2)} \texttt{*a}
600
- may be removed by
601
- \texttt{pop_heap()},
602
- or a new element added by
603
- \texttt{push_heap()},
604
- in
605
- $\mathcal{O}(\log(N))$
606
- time.`**
607
 
608
  These properties make heaps useful as priority queues.
609
 
610
  `make_heap()`
611
 
@@ -621,19 +937,19 @@ template<class RandomAccessIterator>
621
  template<class RandomAccessIterator, class Compare>
622
  void push_heap(RandomAccessIterator first, RandomAccessIterator last,
623
  Compare comp);
624
  ```
625
 
626
- *Effects:* Places the value in the location `last - 1` into the
627
- resulting heap \[`first`, `last`).
628
-
629
  *Requires:* The range \[`first`, `last - 1`) shall be a valid heap. The
630
  type of `*first` shall satisfy the `MoveConstructible` requirements
631
- (Table  [[moveconstructible]]) and the `MoveAssignable` requirements
632
- (Table  [[moveassignable]]).
633
 
634
- *Complexity:* At most `log(last - first)` comparisons.
 
 
 
635
 
636
  #### `pop_heap` <a id="pop.heap">[[pop.heap]]</a>
637
 
638
  ``` cpp
639
  template<class RandomAccessIterator>
@@ -646,17 +962,17 @@ template<class RandomAccessIterator, class Compare>
646
 
647
  *Requires:* The range \[`first`, `last`) shall be a valid non-empty
648
  heap. `RandomAccessIterator` shall satisfy the requirements of
649
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
650
  shall satisfy the requirements of `MoveConstructible`
651
- (Table  [[moveconstructible]]) and of `MoveAssignable`
652
- (Table  [[moveassignable]]).
653
 
654
  *Effects:* Swaps the value in the location `first` with the value in the
655
  location `last - 1` and makes \[`first`, `last - 1`) into a heap.
656
 
657
- *Complexity:* At most `2 * log(last - first)` comparisons.
658
 
659
  #### `make_heap` <a id="make.heap">[[make.heap]]</a>
660
 
661
  ``` cpp
662
  template<class RandomAccessIterator>
@@ -665,17 +981,17 @@ template<class RandomAccessIterator>
665
  template<class RandomAccessIterator, class Compare>
666
  void make_heap(RandomAccessIterator first, RandomAccessIterator last,
667
  Compare comp);
668
  ```
669
 
670
- *Effects:* Constructs a heap out of the range \[`first`, `last`).
671
-
672
  *Requires:* The type of `*first` shall satisfy the `MoveConstructible`
673
- requirements (Table  [[moveconstructible]]) and the `MoveAssignable`
674
- requirements (Table  [[moveassignable]]).
675
 
676
- *Complexity:* At most `3 * (last - first)` comparisons.
 
 
677
 
678
  #### `sort_heap` <a id="sort.heap">[[sort.heap]]</a>
679
 
680
  ``` cpp
681
  template<class RandomAccessIterator>
@@ -684,47 +1000,76 @@ template<class RandomAccessIterator>
684
  template<class RandomAccessIterator, class Compare>
685
  void sort_heap(RandomAccessIterator first, RandomAccessIterator last,
686
  Compare comp);
687
  ```
688
 
689
- *Effects:* Sorts elements in the heap \[`first`, `last`).
690
-
691
  *Requires:* The range \[`first`, `last`) shall be a valid heap.
692
  `RandomAccessIterator` shall satisfy the requirements of
693
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
694
  shall satisfy the requirements of `MoveConstructible`
695
- (Table  [[moveconstructible]]) and of `MoveAssignable`
696
- (Table  [[moveassignable]]).
697
 
698
- *Complexity:* At most N log(N) comparisons (where `N == last - first`).
 
 
699
 
700
  #### `is_heap` <a id="is.heap">[[is.heap]]</a>
701
 
702
  ``` cpp
703
  template<class RandomAccessIterator>
704
  bool is_heap(RandomAccessIterator first, RandomAccessIterator last);
705
  ```
706
 
707
  *Returns:* `is_heap_until(first, last) == last`
708
 
 
 
 
 
 
 
 
 
 
709
  ``` cpp
710
  template<class RandomAccessIterator, class Compare>
711
  bool is_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
712
  ```
713
 
714
  *Returns:* `is_heap_until(first, last, comp) == last`
715
 
 
 
 
 
 
 
 
 
 
 
 
 
716
  ``` cpp
717
  template<class RandomAccessIterator>
718
  RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last);
 
 
 
 
719
  template<class RandomAccessIterator, class Compare>
720
  RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last,
721
  Compare comp);
 
 
 
 
722
  ```
723
 
724
- *Returns:* If `distance(first, last) < 2`, returns `last`. Otherwise,
725
- returns the last iterator `i` in \[`first`, `last`\] for which the range
726
  \[`first`, `i`) is a heap.
727
 
728
  *Complexity:* Linear.
729
 
730
  ### Minimum and maximum <a id="alg.min.max">[[alg.min.max]]</a>
@@ -733,68 +1078,76 @@ returns the last iterator `i` in \[`first`, `last`\] for which the range
733
  template<class T> constexpr const T& min(const T& a, const T& b);
734
  template<class T, class Compare>
735
  constexpr const T& min(const T& a, const T& b, Compare comp);
736
  ```
737
 
738
- *Requires:* Type `T` is `LessThanComparable`
739
- (Table  [[lessthancomparable]]).
740
 
741
  *Returns:* The smaller value.
742
 
743
  *Remarks:* Returns the first argument when the arguments are equivalent.
744
 
 
 
745
  ``` cpp
746
  template<class T>
747
  constexpr T min(initializer_list<T> t);
748
  template<class T, class Compare>
749
  constexpr T min(initializer_list<T> t, Compare comp);
750
  ```
751
 
752
- *Requires:* `T` is `LessThanComparable` and `CopyConstructible` and
753
- `t.size() > 0`.
754
 
755
  *Returns:* The smallest value in the initializer_list.
756
 
757
  *Remarks:* Returns a copy of the leftmost argument when several
758
  arguments are equivalent to the smallest. 
759
 
 
 
760
  ``` cpp
761
  template<class T> constexpr const T& max(const T& a, const T& b);
762
  template<class T, class Compare>
763
  constexpr const T& max(const T& a, const T& b, Compare comp);
764
  ```
765
 
766
- *Requires:* Type `T` is `LessThanComparable`
767
- (Table  [[lessthancomparable]]).
768
 
769
  *Returns:* The larger value.
770
 
771
  *Remarks:* Returns the first argument when the arguments are equivalent.
772
 
 
 
773
  ``` cpp
774
  template<class T>
775
  constexpr T max(initializer_list<T> t);
776
  template<class T, class Compare>
777
  constexpr T max(initializer_list<T> t, Compare comp);
778
  ```
779
 
780
- *Requires:* `T` is `LessThanComparable` and `CopyConstructible` and
781
- `t.size() > 0`.
782
 
783
  *Returns:* The largest value in the initializer_list.
784
 
785
  *Remarks:* Returns a copy of the leftmost argument when several
786
  arguments are equivalent to the largest.
787
 
 
 
788
  ``` cpp
789
  template<class T> constexpr pair<const T&, const T&> minmax(const T& a, const T& b);
790
  template<class T, class Compare>
791
  constexpr pair<const T&, const T&> minmax(const T& a, const T& b, Compare comp);
792
  ```
793
 
794
- *Requires:* Type `T` shall be `LessThanComparable`
795
- (Table  [[lessthancomparable]]).
796
 
797
  *Returns:* `pair<const T&, const T&>(b, a)` if `b` is smaller than `a`,
798
  and `pair<const T&, const T&>(a, b)` otherwise.
799
 
800
  *Remarks:* Returns `pair<const T&, const T&>(a, b)` when the arguments
@@ -807,116 +1160,179 @@ template<class T>
807
  constexpr pair<T, T> minmax(initializer_list<T> t);
808
  template<class T, class Compare>
809
  constexpr pair<T, T> minmax(initializer_list<T> t, Compare comp);
810
  ```
811
 
812
- *Requires:* `T` is `LessThanComparable` and `CopyConstructible` and
813
- `t.size() > 0`.
814
 
815
  *Returns:* `pair<T, T>(x, y)`, where `x` has the smallest and `y` has
816
  the largest value in the initializer list.
817
 
818
  *Remarks:* `x` is a copy of the leftmost argument when several arguments
819
  are equivalent to the smallest. `y` is a copy of the rightmost argument
820
  when several arguments are equivalent to the largest.
821
 
822
- *Complexity:* At most `(3/2) * t.size()` applications of the
823
- corresponding predicate.
824
 
825
  ``` cpp
826
  template<class ForwardIterator>
827
- ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
 
 
 
 
828
 
829
  template<class ForwardIterator, class Compare>
830
- ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
 
 
 
 
831
  Compare comp);
832
  ```
833
 
834
  *Returns:* The first iterator `i` in the range \[`first`, `last`) such
835
  that for every iterator `j` in the range \[`first`, `last`) the
836
  following corresponding conditions hold: `!(*j < *i)` or
837
  `comp(*j, *i) == false`. Returns `last` if `first == last`.
838
 
839
- *Complexity:* Exactly `max((last - first) - 1, 0)` applications of the
840
  corresponding comparisons.
841
 
842
  ``` cpp
843
  template<class ForwardIterator>
844
- ForwardIterator max_element(ForwardIterator first, ForwardIterator last);
 
 
 
 
845
  template<class ForwardIterator, class Compare>
846
- ForwardIterator max_element(ForwardIterator first, ForwardIterator last,
 
 
 
 
847
  Compare comp);
848
  ```
849
 
850
  *Returns:* The first iterator `i` in the range \[`first`, `last`) such
851
  that for every iterator `j` in the range \[`first`, `last`) the
852
  following corresponding conditions hold: `!(*i < *j)` or
853
  `comp(*i, *j) == false`. Returns `last` if `first == last`.
854
 
855
- *Complexity:* Exactly `max((last - first) - 1, 0)` applications of the
856
  corresponding comparisons.
857
 
858
  ``` cpp
859
  template<class ForwardIterator>
860
- pair<ForwardIterator, ForwardIterator>
861
  minmax_element(ForwardIterator first, ForwardIterator last);
862
- template<class ForwardIterator, class Compare>
863
  pair<ForwardIterator, ForwardIterator>
 
 
 
 
 
864
  minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
 
 
 
 
865
  ```
866
 
867
  *Returns:* `make_pair(first, first)` if \[`first`, `last`) is empty,
868
  otherwise `make_pair(m, M)`, where `m` is the first iterator in
869
  \[`first`, `last`) such that no iterator in the range refers to a
870
- smaller element, and where `M` is the last iterator in \[`first`,
871
  `last`) such that no iterator in the range refers to a larger element.
872
 
873
- *Complexity:* At most $max(\lfloor{\frac{3}{2}} (N-1)\rfloor, 0)$
874
- applications of the corresponding predicate, where N is
875
- `distance(first, last)`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
876
 
877
  ### Lexicographical comparison <a id="alg.lex.comparison">[[alg.lex.comparison]]</a>
878
 
879
  ``` cpp
880
  template<class InputIterator1, class InputIterator2>
881
  bool
882
  lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
883
  InputIterator2 first2, InputIterator2 last2);
 
 
 
 
 
884
 
885
  template<class InputIterator1, class InputIterator2, class Compare>
886
  bool
887
  lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
888
  InputIterator2 first2, InputIterator2 last2,
889
  Compare comp);
 
 
 
 
 
 
890
  ```
891
 
892
  *Returns:* `true` if the sequence of elements defined by the range
893
  \[`first1`, `last1`) is lexicographically less than the sequence of
894
  elements defined by the range \[`first2`, `last2`) and `false`
895
  otherwise.
896
 
897
- *Complexity:* At most `2*min((last1 - first1), (last2 - first2))`
898
  applications of the corresponding comparison.
899
 
900
  *Remarks:* If two sequences have the same number of elements and their
901
- corresponding elements are equivalent, then neither sequence is
902
  lexicographically less than the other. If one sequence is a prefix of
903
  the other, then the shorter sequence is lexicographically less than the
904
  longer sequence. Otherwise, the lexicographical comparison of the
905
  sequences yields the same result as the comparison of the first
906
  corresponding pair of elements that are not equivalent.
907
 
 
 
 
 
908
  ``` cpp
909
- for ( ; first1 != last1 && first2 != last2 ; ++first1, ++first2) {
910
  if (*first1 < *first2) return true;
911
  if (*first2 < *first1) return false;
912
  }
913
  return first1 == last1 && first2 != last2;
914
  ```
915
 
916
- *Remarks:*  An empty sequence is lexicographically less than any
917
- non-empty sequence, but not less than any empty sequence.
 
 
918
 
919
  ### Permutation generators <a id="alg.permutation.generators">[[alg.permutation.generators]]</a>
920
 
921
  ``` cpp
922
  template<class BidirectionalIterator>
@@ -926,19 +1342,21 @@ template<class BidirectionalIterator>
926
  template<class BidirectionalIterator, class Compare>
927
  bool next_permutation(BidirectionalIterator first,
928
  BidirectionalIterator last, Compare comp);
929
  ```
930
 
 
 
 
931
  *Effects:* Takes a sequence defined by the range \[`first`, `last`) and
932
  transforms it into the next permutation. The next permutation is found
933
  by assuming that the set of all permutations is lexicographically sorted
934
- with respect to `operator<` or `comp`. If such a permutation exists, it
935
- returns `true`. Otherwise, it transforms the sequence into the smallest
936
- permutation, that is, the ascendingly sorted one, and returns `false`.
937
 
938
- *Requires:* `BidirectionalIterator` shall satisfy the requirements of
939
- `ValueSwappable` ([[swappable.requirements]]).
 
940
 
941
  *Complexity:* At most `(last - first) / 2` swaps.
942
 
943
  ``` cpp
944
  template<class BidirectionalIterator>
@@ -948,19 +1366,19 @@ template<class BidirectionalIterator>
948
  template<class BidirectionalIterator, class Compare>
949
  bool prev_permutation(BidirectionalIterator first,
950
  BidirectionalIterator last, Compare comp);
951
  ```
952
 
 
 
 
953
  *Effects:* Takes a sequence defined by the range \[`first`, `last`) and
954
  transforms it into the previous permutation. The previous permutation is
955
  found by assuming that the set of all permutations is lexicographically
956
  sorted with respect to `operator<` or `comp`.
957
 
958
  *Returns:* `true` if such a permutation exists. Otherwise, it transforms
959
  the sequence into the largest permutation, that is, the descendingly
960
  sorted one, and returns `false`.
961
 
962
- *Requires:* `BidirectionalIterator` shall satisfy the requirements of
963
- `ValueSwappable` ([[swappable.requirements]]).
964
-
965
  *Complexity:* At most `(last - first) / 2` swaps.
966
 
 
14
  non-constant function through the dereferenced iterator.
15
 
16
  For all algorithms that take `Compare`, there is a version that uses
17
  `operator<` instead. That is, `comp(*i, *j) != false` defaults to
18
  `*i < *j != false`. For algorithms other than those described in 
19
+ [[alg.binary.search]], `comp` shall induce a strict weak ordering on the
20
+ values.
21
 
22
  The term *strict* refers to the requirement of an irreflexive relation
23
  (`!comp(x, x)` for all `x`), and the term *weak* to requirements that
24
  are not as strong as those for a total ordering, but stronger than those
25
  for a partial ordering. If we define `equiv(a, b)` as
26
  `!comp(a, b) && !comp(b, a)`, then the requirements are that `comp` and
27
  `equiv` both be transitive relations:
28
 
29
  - `comp(a, b) && comp(b, c)` implies `comp(a, c)`
30
+ - `equiv(a, b) && equiv(b, c)` implies `equiv(a, c)`
31
+
32
+ [*Note 1*:
33
+
34
+ Under these conditions, it can be shown that
35
+
36
  - `equiv` is an equivalence relation
37
  - `comp` induces a well-defined relation on the equivalence classes
38
  determined by `equiv`
39
  - The induced relation is a strict total ordering.
40
 
41
+ — *end note*]
42
+
43
  A sequence is *sorted with respect to a comparator* `comp` if for every
44
  iterator `i` pointing to the sequence and every non-negative integer `n`
45
  such that `i + n` is a valid iterator pointing to an element of the
46
  sequence, `comp(*(i + n), *i) == false`.
47
 
48
  A sequence \[`start`, `finish`) is *partitioned with respect to an
49
  expression* `f(e)` if there exists an integer `n` such that for all
50
+ `0 <= i < (finish - start)`, `f(*(start + i))` is `true` if and only if
51
+ `i < n`.
52
 
53
  In the descriptions of the functions that deal with ordering
54
  relationships we frequently use a notion of equivalence to describe
55
  concepts such as stability. The equivalence to which we refer is not
56
  necessarily an `operator==`, but an equivalence relation induced by the
 
62
  #### `sort` <a id="sort">[[sort]]</a>
63
 
64
  ``` cpp
65
  template<class RandomAccessIterator>
66
  void sort(RandomAccessIterator first, RandomAccessIterator last);
67
+ template<class ExecutionPolicy, class RandomAccessIterator>
68
+ void sort(ExecutionPolicy&& exec,
69
+ RandomAccessIterator first, RandomAccessIterator last);
70
 
71
  template<class RandomAccessIterator, class Compare>
72
  void sort(RandomAccessIterator first, RandomAccessIterator last,
73
  Compare comp);
74
+ template<class ExecutionPolicy, class RandomAccessIterator, class Compare>
75
+ void sort(ExecutionPolicy&& exec,
76
+ RandomAccessIterator first, RandomAccessIterator last,
77
+ Compare comp);
78
  ```
79
 
 
 
80
  *Requires:* `RandomAccessIterator` shall satisfy the requirements of
81
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
82
  shall satisfy the requirements of `MoveConstructible`
83
+ (Table  [[tab:moveconstructible]]) and of `MoveAssignable`
84
+ (Table  [[tab:moveassignable]]).
85
 
86
+ *Effects:* Sorts the elements in the range \[`first`, `last`).
87
+
88
+ *Complexity:* 𝑂(N log N) comparisons, where N = `last - first`.
89
 
90
  #### `stable_sort` <a id="stable.sort">[[stable.sort]]</a>
91
 
92
  ``` cpp
93
  template<class RandomAccessIterator>
94
  void stable_sort(RandomAccessIterator first, RandomAccessIterator last);
95
+ template<class ExecutionPolicy, class RandomAccessIterator>
96
+ void stable_sort(ExecutionPolicy&& exec,
97
+ RandomAccessIterator first, RandomAccessIterator last);
98
 
99
  template<class RandomAccessIterator, class Compare>
100
  void stable_sort(RandomAccessIterator first, RandomAccessIterator last,
101
  Compare comp);
102
+ template<class ExecutionPolicy, class RandomAccessIterator, class Compare>
103
+ void stable_sort(ExecutionPolicy&& exec,
104
+ RandomAccessIterator first, RandomAccessIterator last,
105
+ Compare comp);
106
  ```
107
 
 
 
108
  *Requires:* `RandomAccessIterator` shall satisfy the requirements of
109
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
110
  shall satisfy the requirements of `MoveConstructible`
111
+ (Table  [[tab:moveconstructible]]) and of `MoveAssignable`
112
+ (Table  [[tab:moveassignable]]).
113
 
114
+ *Effects:* Sorts the elements in the range \[`first`, `last`).
115
+
116
+ *Complexity:* At most N log²(N) comparisons, where N = `last - first`,
117
+ but only N log N comparisons if there is enough extra memory.
118
 
119
  *Remarks:* Stable ([[algorithm.stable]]).
120
 
121
  #### `partial_sort` <a id="partial.sort">[[partial.sort]]</a>
122
 
123
  ``` cpp
124
  template<class RandomAccessIterator>
125
  void partial_sort(RandomAccessIterator first,
126
  RandomAccessIterator middle,
127
  RandomAccessIterator last);
128
+ template<class ExecutionPolicy, class RandomAccessIterator>
129
+ void partial_sort(ExecutionPolicy&& exec,
130
+ RandomAccessIterator first,
131
+ RandomAccessIterator middle,
132
+ RandomAccessIterator last);
133
 
134
  template<class RandomAccessIterator, class Compare>
135
  void partial_sort(RandomAccessIterator first,
136
  RandomAccessIterator middle,
137
  RandomAccessIterator last,
138
  Compare comp);
139
+ template<class ExecutionPolicy, class RandomAccessIterator, class Compare>
140
+ void partial_sort(ExecutionPolicy&& exec,
141
+ RandomAccessIterator first,
142
+ RandomAccessIterator middle,
143
+ RandomAccessIterator last,
144
+ Compare comp);
145
  ```
146
 
147
+ *Requires:* `RandomAccessIterator` shall satisfy the requirements of
148
+ `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
149
+ shall satisfy the requirements of `MoveConstructible`
150
+ (Table  [[tab:moveconstructible]]) and of `MoveAssignable`
151
+ (Table  [[tab:moveassignable]]).
152
+
153
  *Effects:* Places the first `middle - first` sorted elements from the
154
  range \[`first`, `last`) into the range \[`first`, `middle`). The rest
155
  of the elements in the range \[`middle`, `last`) are placed in an
156
  unspecified order.
157
 
158
+ *Complexity:* Approximately `(last - first) * log(middle - first)`
159
+ comparisons.
 
 
 
 
 
 
160
 
161
  #### `partial_sort_copy` <a id="partial.sort.copy">[[partial.sort.copy]]</a>
162
 
163
  ``` cpp
164
  template<class InputIterator, class RandomAccessIterator>
165
  RandomAccessIterator
166
  partial_sort_copy(InputIterator first, InputIterator last,
167
  RandomAccessIterator result_first,
168
  RandomAccessIterator result_last);
169
+ template<class ExecutionPolicy, class ForwardIterator, class RandomAccessIterator>
170
+ RandomAccessIterator
171
+ partial_sort_copy(ExecutionPolicy&& exec,
172
+ ForwardIterator first, ForwardIterator last,
173
+ RandomAccessIterator result_first,
174
+ RandomAccessIterator result_last);
175
 
176
  template<class InputIterator, class RandomAccessIterator,
177
  class Compare>
178
  RandomAccessIterator
179
  partial_sort_copy(InputIterator first, InputIterator last,
180
  RandomAccessIterator result_first,
181
  RandomAccessIterator result_last,
182
  Compare comp);
183
+ template<class ExecutionPolicy, class ForwardIterator, class RandomAccessIterator,
184
+ class Compare>
185
+ RandomAccessIterator
186
+ partial_sort_copy(ExecutionPolicy&& exec,
187
+ ForwardIterator first, ForwardIterator last,
188
+ RandomAccessIterator result_first,
189
+ RandomAccessIterator result_last,
190
+ Compare comp);
191
  ```
192
 
193
+ *Requires:* `RandomAccessIterator` shall satisfy the requirements of
194
+ `ValueSwappable` ([[swappable.requirements]]). The type of
195
+ `*result_first` shall satisfy the requirements of `MoveConstructible`
196
+ (Table  [[tab:moveconstructible]]) and of `MoveAssignable`
197
+ (Table  [[tab:moveassignable]]).
198
+
199
  *Effects:* Places the first
200
  `min(last - first, result_last - result_first)` sorted elements into the
201
  range \[`result_first`,
202
  `result_first + min(last - first, result_last - result_first)`).
203
 
204
  *Returns:* The smaller of: `result_last` or
205
  `result_first + (last - first)`.
206
 
 
 
 
 
 
 
207
  *Complexity:* Approximately
208
  `(last - first) * log(min(last - first, result_last - result_first))`
209
  comparisons.
210
 
211
  #### `is_sorted` <a id="is.sorted">[[is.sorted]]</a>
 
215
  bool is_sorted(ForwardIterator first, ForwardIterator last);
216
  ```
217
 
218
  *Returns:* `is_sorted_until(first, last) == last`
219
 
220
+ ``` cpp
221
+ template<class ExecutionPolicy, class ForwardIterator>
222
+ bool is_sorted(ExecutionPolicy&& exec,
223
+ ForwardIterator first, ForwardIterator last);
224
+ ```
225
+
226
+ *Returns:*
227
+ `is_sorted_until(std::forward<ExecutionPolicy>(exec), first, last) == last`
228
+
229
  ``` cpp
230
  template<class ForwardIterator, class Compare>
231
  bool is_sorted(ForwardIterator first, ForwardIterator last,
232
  Compare comp);
233
  ```
234
 
235
  *Returns:* `is_sorted_until(first, last, comp) == last`
236
 
237
+ ``` cpp
238
+ template<class ExecutionPolicy, class ForwardIterator, class Compare>
239
+ bool is_sorted(ExecutionPolicy&& exec,
240
+ ForwardIterator first, ForwardIterator last,
241
+ Compare comp);
242
+ ```
243
+
244
+ *Returns:*
245
+
246
+ ``` cpp
247
+ is_sorted_until(std::forward<ExecutionPolicy>(exec), first, last, comp) == last
248
+ ```
249
+
250
  ``` cpp
251
  template<class ForwardIterator>
252
  ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last);
253
+ template<class ExecutionPolicy, class ForwardIterator>
254
+ ForwardIterator is_sorted_until(ExecutionPolicy&& exec,
255
+ ForwardIterator first, ForwardIterator last);
256
+
257
  template<class ForwardIterator, class Compare>
258
  ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last,
259
  Compare comp);
260
+ template<class ExecutionPolicy, class ForwardIterator, class Compare>
261
+ ForwardIterator is_sorted_until(ExecutionPolicy&& exec,
262
+ ForwardIterator first, ForwardIterator last,
263
+ Compare comp);
264
  ```
265
 
266
+ *Returns:* If `(last - first) < 2`, returns `last`. Otherwise, returns
267
+ the last iterator `i` in \[`first`, `last`\] for which the range
268
  \[`first`, `i`) is sorted.
269
 
270
  *Complexity:* Linear.
271
 
272
  ### Nth element <a id="alg.nth.element">[[alg.nth.element]]</a>
273
 
274
  ``` cpp
275
  template<class RandomAccessIterator>
276
  void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
277
  RandomAccessIterator last);
278
+ template<class ExecutionPolicy, class RandomAccessIterator>
279
+ void nth_element(ExecutionPolicy&& exec,
280
+ RandomAccessIterator first, RandomAccessIterator nth,
281
+ RandomAccessIterator last);
282
 
283
  template<class RandomAccessIterator, class Compare>
284
  void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
285
  RandomAccessIterator last, Compare comp);
286
+ template<class ExecutionPolicy, class RandomAccessIterator, class Compare>
287
+ void nth_element(ExecutionPolicy&& exec,
288
+ RandomAccessIterator first, RandomAccessIterator nth,
289
+ RandomAccessIterator last, Compare comp);
290
  ```
291
 
 
 
 
 
 
 
292
  *Requires:* `RandomAccessIterator` shall satisfy the requirements of
293
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
294
  shall satisfy the requirements of `MoveConstructible`
295
+ (Table  [[tab:moveconstructible]]) and of `MoveAssignable`
296
+ (Table  [[tab:moveassignable]]).
297
 
298
+ *Effects:* After `nth_element` the element in the position pointed to by
299
+ `nth` is the element that would be in that position if the whole range
300
+ were sorted, unless `nth == last`. Also for every iterator `i` in the
301
+ range \[`first`, `nth`) and every iterator `j` in the range \[`nth`,
302
+ `last`) it holds that: `!(*j < *i)` or `comp(*j, *i) == false`.
303
+
304
+ *Complexity:* For the overloads with no `ExecutionPolicy`, linear on
305
+ average. For the overloads with an `ExecutionPolicy`, 𝑂(N) applications
306
+ of the predicate, and 𝑂(N log N) swaps, where N = `last - first`.
307
 
308
  ### Binary search <a id="alg.binary.search">[[alg.binary.search]]</a>
309
 
310
  All of the algorithms in this section are versions of binary search and
311
  assume that the sequence being searched is partitioned with respect to
 
425
  `!(*i < value) && !(value < *i)` or
426
  `comp(*i, value) == false && comp(value, *i) == false`.
427
 
428
  *Complexity:* At most log₂(`last - first`) + 𝑂(1) comparisons.
429
 
430
+ ### Partitions <a id="alg.partitions">[[alg.partitions]]</a>
431
+
432
+ ``` cpp
433
+ template <class InputIterator, class Predicate>
434
+ bool is_partitioned(InputIterator first, InputIterator last, Predicate pred);
435
+ template <class ExecutionPolicy, class ForwardIterator, class Predicate>
436
+ bool is_partitioned(ExecutionPolicy&& exec,
437
+ ForwardIterator first, ForwardIterator last, Predicate pred);
438
+ ```
439
+
440
+ *Requires:* For the overload with no `ExecutionPolicy`,
441
+ `InputIterator`’s value type shall be convertible to `Predicate`’s
442
+ argument type. For the overload with an `ExecutionPolicy`,
443
+ `ForwardIterator`’s value type shall be convertible to `Predicate`’s
444
+ argument type.
445
+
446
+ *Returns:* `true` if \[`first`, `last`) is empty or if \[`first`,
447
+ `last`) is partitioned by `pred`, i.e. if all elements that satisfy
448
+ `pred` appear before those that do not.
449
+
450
+ *Complexity:* Linear. At most `last - first` applications of `pred`.
451
+
452
+ ``` cpp
453
+ template<class ForwardIterator, class Predicate>
454
+ ForwardIterator
455
+ partition(ForwardIterator first, ForwardIterator last, Predicate pred);
456
+ template<class ExecutionPolicy, class ForwardIterator, class Predicate>
457
+ ForwardIterator
458
+ partition(ExecutionPolicy&& exec,
459
+ ForwardIterator first, ForwardIterator last, Predicate pred);
460
+ ```
461
+
462
+ *Requires:* `ForwardIterator` shall satisfy the requirements of
463
+ `ValueSwappable` ([[swappable.requirements]]).
464
+
465
+ *Effects:* Places all the elements in the range \[`first`, `last`) that
466
+ satisfy `pred` before all the elements that do not satisfy it.
467
+
468
+ *Returns:* An iterator `i` such that for every iterator `j` in the range
469
+ \[`first`, `i`) `pred(*j) != false`, and for every iterator `k` in the
470
+ range \[`i`, `last`), `pred(*k) == false`.
471
+
472
+ *Complexity:* Let N = `last - first`:
473
+
474
+ - For the overload with no `ExecutionPolicy`, exactly N applications of
475
+ the predicate. At most N / 2 swaps if `ForwardIterator` meets the
476
+ `BidirectionalIterator` requirements and at most N swaps otherwise.
477
+ - For the overload with an `ExecutionPolicy`, 𝑂(N log N) swaps and 𝑂(N)
478
+ applications of the predicate.
479
+
480
+ ``` cpp
481
+ template<class BidirectionalIterator, class Predicate>
482
+ BidirectionalIterator
483
+ stable_partition(BidirectionalIterator first, BidirectionalIterator last,
484
+ Predicate pred);
485
+ template<class ExecutionPolicy, class BidirectionalIterator, class Predicate>
486
+ BidirectionalIterator
487
+ stable_partition(ExecutionPolicy&& exec,
488
+ BidirectionalIterator first, BidirectionalIterator last,
489
+ Predicate pred);
490
+ ```
491
+
492
+ *Requires:* `BidirectionalIterator` shall satisfy the requirements of
493
+ `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
494
+ shall satisfy the requirements of `MoveConstructible`
495
+ (Table  [[tab:moveconstructible]]) and of `MoveAssignable`
496
+ (Table  [[tab:moveassignable]]).
497
+
498
+ *Effects:* Places all the elements in the range \[`first`, `last`) that
499
+ satisfy `pred` before all the elements that do not satisfy it.
500
+
501
+ *Returns:* An iterator `i` such that for every iterator `j` in the range
502
+ \[`first`, `i`), `pred(*j) != false`, and for every iterator `k` in the
503
+ range \[`i`, `last`), `pred(*k) == false`. The relative order of the
504
+ elements in both groups is preserved.
505
+
506
+ *Complexity:* Let N = `last - first`:
507
+
508
+ - For the overload with no `ExecutionPolicy`, at most N log N swaps, but
509
+ only 𝑂(N) swaps if there is enough extra memory. Exactly N
510
+ applications of the predicate.
511
+ - For the overload with an `ExecutionPolicy`, 𝑂(N log N) swaps and 𝑂(N)
512
+ applications of the predicate.
513
+
514
+ ``` cpp
515
+ template <class InputIterator, class OutputIterator1,
516
+ class OutputIterator2, class Predicate>
517
+ pair<OutputIterator1, OutputIterator2>
518
+ partition_copy(InputIterator first, InputIterator last,
519
+ OutputIterator1 out_true, OutputIterator2 out_false,
520
+ Predicate pred);
521
+ template <class ExecutionPolicy, class ForwardIterator, class ForwardIterator1,
522
+ class ForwardIterator2, class Predicate>
523
+ pair<ForwardIterator1, ForwardIterator2>
524
+ partition_copy(ExecutionPolicy&& exec,
525
+ ForwardIterator first, ForwardIterator last,
526
+ ForwardIterator1 out_true, ForwardIterator2 out_false,
527
+ Predicate pred);
528
+ ```
529
+
530
+ *Requires:*
531
+
532
+ - For the overload with no `ExecutionPolicy`, `InputIterator`’s value
533
+ type shall be `CopyAssignable` (Table  [[tab:copyassignable]]), and
534
+ shall be writable ([[iterator.requirements.general]]) to the
535
+ `out_true` and `out_false` `OutputIterator`s, and shall be convertible
536
+ to `Predicate`’s argument type.
537
+ - For the overload with an `ExecutionPolicy`, `ForwardIterator`’s value
538
+ type shall be `CopyAssignable`, and shall be writable to the
539
+ `out_true` and `out_false` `ForwardIterator`s, and shall be
540
+ convertible to `Predicate`’s argument type. \[*Note 1*: There may be a
541
+ performance cost if `ForwardIterator`’s value type is not
542
+ `CopyConstructible`. — *end note*]
543
+ - For both overloads, the input range shall not overlap with either of
544
+ the output ranges.
545
+
546
+ *Effects:* For each iterator `i` in \[`first`, `last`), copies `*i` to
547
+ the output range beginning with `out_true` if `pred(*i)` is `true`, or
548
+ to the output range beginning with `out_false` otherwise.
549
+
550
+ *Returns:* A pair `p` such that `p.first` is the end of the output range
551
+ beginning at `out_true` and `p.second` is the end of the output range
552
+ beginning at `out_false`.
553
+
554
+ *Complexity:* Exactly `last - first` applications of `pred`.
555
+
556
+ ``` cpp
557
+ template<class ForwardIterator, class Predicate>
558
+ ForwardIterator partition_point(ForwardIterator first,
559
+ ForwardIterator last,
560
+ Predicate pred);
561
+ ```
562
+
563
+ *Requires:* `ForwardIterator`’s value type shall be convertible to
564
+ `Predicate`’s argument type. \[`first`, `last`) shall be partitioned by
565
+ `pred`, i.e. all elements that satisfy `pred` shall appear before those
566
+ that do not.
567
+
568
+ *Returns:* An iterator `mid` such that `all_of(first, mid, pred)` and
569
+ `none_of(mid, last, pred)` are both `true`.
570
+
571
+ *Complexity:* 𝑂(log(`last - first`)) applications of `pred`.
572
+
573
  ### Merge <a id="alg.merge">[[alg.merge]]</a>
574
 
575
  ``` cpp
576
  template<class InputIterator1, class InputIterator2,
577
  class OutputIterator>
578
  OutputIterator
579
  merge(InputIterator1 first1, InputIterator1 last1,
580
  InputIterator2 first2, InputIterator2 last2,
581
  OutputIterator result);
582
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
583
+ class ForwardIterator>
584
+ ForwardIterator
585
+ merge(ExecutionPolicy&& exec,
586
+ ForwardIterator1 first1, ForwardIterator1 last1,
587
+ ForwardIterator2 first2, ForwardIterator2 last2,
588
+ ForwardIterator result);
589
 
590
  template<class InputIterator1, class InputIterator2,
591
  class OutputIterator, class Compare>
592
  OutputIterator
593
  merge(InputIterator1 first1, InputIterator1 last1,
594
  InputIterator2 first2, InputIterator2 last2,
595
  OutputIterator result, Compare comp);
596
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
597
+ class ForwardIterator, class Compare>
598
+ ForwardIterator
599
+ merge(ExecutionPolicy&& exec,
600
+ ForwardIterator1 first1, ForwardIterator1 last1,
601
+ ForwardIterator2 first2, ForwardIterator2 last2,
602
+ ForwardIterator result, Compare comp);
603
  ```
604
 
605
+ *Requires:* The ranges \[`first1`, `last1`) and \[`first2`, `last2`)
606
+ shall be sorted with respect to `operator<` or `comp`. The resulting
607
+ range shall not overlap with either of the original ranges.
608
+
609
  *Effects:*  Copies all the elements of the two ranges \[`first1`,
610
  `last1`) and \[`first2`, `last2`) into the range \[`result`,
611
  `result_last`), where `result_last` is
612
  `result + (last1 - first1) + (last2 - first2)`, such that the resulting
613
  range satisfies `is_sorted(result, result_last)` or
614
  `is_sorted(result, result_last, comp)`, respectively.
615
 
 
 
 
 
616
  *Returns:* `result + (last1 - first1) + (last2 - first2)`.
617
 
618
+ *Complexity:* Let N = `(last1 - first1) + (last2 - first2)`:
619
+
620
+ - For the overloads with no `ExecutionPolicy`, at most N - 1
621
  comparisons.
622
+ - For the overloads with an `ExecutionPolicy`, 𝑂(N) comparisons.
623
 
624
  *Remarks:* Stable ([[algorithm.stable]]).
625
 
626
  ``` cpp
627
  template<class BidirectionalIterator>
628
  void inplace_merge(BidirectionalIterator first,
629
  BidirectionalIterator middle,
630
  BidirectionalIterator last);
631
+ template<class ExecutionPolicy, class BidirectionalIterator>
632
+ void inplace_merge(ExecutionPolicy&& exec,
633
+ BidirectionalIterator first,
634
+ BidirectionalIterator middle,
635
+ BidirectionalIterator last);
636
 
637
  template<class BidirectionalIterator, class Compare>
638
  void inplace_merge(BidirectionalIterator first,
639
  BidirectionalIterator middle,
640
  BidirectionalIterator last, Compare comp);
641
+ template<class ExecutionPolicy, class BidirectionalIterator, class Compare>
642
+ void inplace_merge(ExecutionPolicy&& exec,
643
+ BidirectionalIterator first,
644
+ BidirectionalIterator middle,
645
+ BidirectionalIterator last, Compare comp);
646
  ```
647
 
648
+ *Requires:* The ranges \[`first`, `middle`) and \[`middle`, `last`)
649
+ shall be sorted with respect to `operator<` or `comp`.
650
+ `BidirectionalIterator` shall satisfy the requirements of
651
+ `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
652
+ shall satisfy the requirements of `MoveConstructible`
653
+ (Table  [[tab:moveconstructible]]) and of `MoveAssignable`
654
+ (Table  [[tab:moveassignable]]).
655
+
656
  *Effects:* Merges two sorted consecutive ranges \[`first`, `middle`) and
657
  \[`middle`, `last`), putting the result of the merge into the range
658
  \[`first`, `last`). The resulting range will be in non-decreasing order;
659
  that is, for every iterator `i` in \[`first`, `last`) other than
660
  `first`, the condition `*i < *(i - 1)` or, respectively,
661
+ `comp(*i, *(i - 1))` will be `false`.
662
 
663
+ *Complexity:* Let N = `last - first`:
 
 
 
 
 
 
664
 
665
+ - For the overloads with no `ExecutionPolicy`, if enough additional
666
+ memory is available, exactly N - 1 comparisons.
667
+ - For the overloads with no `ExecutionPolicy` if no additional memory is
668
+ available, 𝑂(N log N) comparisons.
669
+ - For the overloads with an `ExecutionPolicy`, 𝑂(N log N) comparisons.
670
 
671
  *Remarks:* Stable ([[algorithm.stable]]).
672
 
673
  ### Set operations on sorted structures <a id="alg.set.operations">[[alg.set.operations]]</a>
674
 
 
683
 
684
  ``` cpp
685
  template<class InputIterator1, class InputIterator2>
686
  bool includes(InputIterator1 first1, InputIterator1 last1,
687
  InputIterator2 first2, InputIterator2 last2);
688
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
689
+ bool includes(ExecutionPolicy&& exec,
690
+ ForwardIterator1 first1, ForwardIterator1 last1,
691
+ ForwardIterator2 first2, ForwardIterator2 last2);
692
 
693
  template<class InputIterator1, class InputIterator2, class Compare>
694
  bool includes(InputIterator1 first1, InputIterator1 last1,
695
  InputIterator2 first2, InputIterator2 last2,
696
  Compare comp);
697
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Compare>
698
+ bool includes(ExecutionPolicy&& exec,
699
+ ForwardIterator1 first1, ForwardIterator1 last1,
700
+ ForwardIterator2 first2, ForwardIterator2 last2,
701
+ Compare comp);
702
  ```
703
 
704
  *Returns:* `true` if \[`first2`, `last2`) is empty or if every element
705
  in the range \[`first2`, `last2`) is contained in the range \[`first1`,
706
  `last1`). Returns `false` otherwise.
 
715
  class OutputIterator>
716
  OutputIterator
717
  set_union(InputIterator1 first1, InputIterator1 last1,
718
  InputIterator2 first2, InputIterator2 last2,
719
  OutputIterator result);
720
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
721
+ class ForwardIterator>
722
+ ForwardIterator
723
+ set_union(ExecutionPolicy&& exec,
724
+ ForwardIterator1 first1, ForwardIterator1 last1,
725
+ ForwardIterator2 first2, ForwardIterator2 last2,
726
+ ForwardIterator result);
727
 
728
  template<class InputIterator1, class InputIterator2,
729
  class OutputIterator, class Compare>
730
  OutputIterator
731
  set_union(InputIterator1 first1, InputIterator1 last1,
732
  InputIterator2 first2, InputIterator2 last2,
733
  OutputIterator result, Compare comp);
734
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
735
+ class ForwardIterator, class Compare>
736
+ ForwardIterator
737
+ set_union(ExecutionPolicy&& exec,
738
+ ForwardIterator1 first1, ForwardIterator1 last1,
739
+ ForwardIterator2 first2, ForwardIterator2 last2,
740
+ ForwardIterator result, Compare comp);
741
  ```
742
 
743
+ *Requires:* The resulting range shall not overlap with either of the
744
+ original ranges.
745
+
746
  *Effects:* Constructs a sorted union of the elements from the two
747
  ranges; that is, the set of elements that are present in one or both of
748
  the ranges.
749
 
 
 
 
750
  *Returns:* The end of the constructed range.
751
 
752
  *Complexity:* At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
753
  comparisons.
754
 
 
766
  class OutputIterator>
767
  OutputIterator
768
  set_intersection(InputIterator1 first1, InputIterator1 last1,
769
  InputIterator2 first2, InputIterator2 last2,
770
  OutputIterator result);
771
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
772
+ class ForwardIterator>
773
+ ForwardIterator
774
+ set_intersection(ExecutionPolicy&& exec,
775
+ ForwardIterator1 first1, ForwardIterator1 last1,
776
+ ForwardIterator2 first2, ForwardIterator2 last2,
777
+ ForwardIterator result);
778
 
779
  template<class InputIterator1, class InputIterator2,
780
  class OutputIterator, class Compare>
781
  OutputIterator
782
  set_intersection(InputIterator1 first1, InputIterator1 last1,
783
  InputIterator2 first2, InputIterator2 last2,
784
  OutputIterator result, Compare comp);
785
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
786
+ class ForwardIterator, class Compare>
787
+ ForwardIterator
788
+ set_intersection(ExecutionPolicy&& exec,
789
+ ForwardIterator1 first1, ForwardIterator1 last1,
790
+ ForwardIterator2 first2, ForwardIterator2 last2,
791
+ ForwardIterator result, Compare comp);
792
  ```
793
 
794
+ *Requires:* The resulting range shall not overlap with either of the
795
+ original ranges.
796
+
797
  *Effects:* Constructs a sorted intersection of the elements from the two
798
  ranges; that is, the set of elements that are present in both of the
799
  ranges.
800
 
 
 
 
801
  *Returns:* The end of the constructed range.
802
 
803
  *Complexity:* At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
804
  comparisons.
805
 
 
815
  class OutputIterator>
816
  OutputIterator
817
  set_difference(InputIterator1 first1, InputIterator1 last1,
818
  InputIterator2 first2, InputIterator2 last2,
819
  OutputIterator result);
820
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
821
+ class ForwardIterator>
822
+ ForwardIterator
823
+ set_difference(ExecutionPolicy&& exec,
824
+ ForwardIterator1 first1, ForwardIterator1 last1,
825
+ ForwardIterator2 first2, ForwardIterator2 last2,
826
+ ForwardIterator result);
827
 
828
  template<class InputIterator1, class InputIterator2,
829
  class OutputIterator, class Compare>
830
  OutputIterator
831
  set_difference(InputIterator1 first1, InputIterator1 last1,
832
  InputIterator2 first2, InputIterator2 last2,
833
  OutputIterator result, Compare comp);
834
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
835
+ class ForwardIterator, class Compare>
836
+ ForwardIterator
837
+ set_difference(ExecutionPolicy&& exec,
838
+ ForwardIterator1 first1, ForwardIterator1 last1,
839
+ ForwardIterator2 first2, ForwardIterator2 last2,
840
+ ForwardIterator result, Compare comp);
841
  ```
842
 
843
+ *Requires:* The resulting range shall not overlap with either of the
844
+ original ranges.
845
+
846
  *Effects:* Copies the elements of the range \[`first1`, `last1`) which
847
  are not present in the range \[`first2`, `last2`) to the range beginning
848
  at `result`. The elements in the constructed range are sorted.
849
 
 
 
 
850
  *Returns:* The end of the constructed range.
851
 
852
  *Complexity:* At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
853
  comparisons.
854
 
 
864
  class OutputIterator>
865
  OutputIterator
866
  set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
867
  InputIterator2 first2, InputIterator2 last2,
868
  OutputIterator result);
869
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
870
+ class ForwardIterator>
871
+ ForwardIterator
872
+ set_symmetric_difference(ExecutionPolicy&& exec,
873
+ ForwardIterator1 first1, ForwardIterator1 last1,
874
+ ForwardIterator2 first2, ForwardIterator2 last2,
875
+ ForwardIterator result);
876
 
877
  template<class InputIterator1, class InputIterator2,
878
  class OutputIterator, class Compare>
879
  OutputIterator
880
  set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
881
  InputIterator2 first2, InputIterator2 last2,
882
  OutputIterator result, Compare comp);
883
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
884
+ class ForwardIterator, class Compare>
885
+ ForwardIterator
886
+ set_symmetric_difference(ExecutionPolicy&& exec,
887
+ ForwardIterator1 first1, ForwardIterator1 last1,
888
+ ForwardIterator2 first2, ForwardIterator2 last2,
889
+ ForwardIterator result, Compare comp);
890
  ```
891
 
892
+ *Requires:* The resulting range shall not overlap with either of the
893
+ original ranges.
894
+
895
  *Effects:* Copies the elements of the range \[`first1`, `last1`) that
896
  are not present in the range \[`first2`, `last2`), and the elements of
897
  the range \[`first2`, `last2`) that are not present in the range
898
  \[`first1`, `last1`) to the range beginning at `result`. The elements in
899
  the constructed range are sorted.
900
 
 
 
 
901
  *Returns:* The end of the constructed range.
902
 
903
  *Complexity:* At most `2 * ((last1 - first1) + (last2 - first2)) - 1`
904
  comparisons.
905
 
 
911
  \[`first2`, `last2`) if m < n.
912
 
913
  ### Heap operations <a id="alg.heap.operations">[[alg.heap.operations]]</a>
914
 
915
  A *heap* is a particular organization of elements in a range between two
916
+ random access iterators \[`a`, `b`) such that:
917
 
918
+ - With `N = b - a`, for all i, 0 < i < N,
919
+ `comp(a[\left \lfloor{\frac{i - 1}{2}}\right \rfloor], a[i])` is
920
+ `false`.
921
+ - `*a` may be removed by `pop_heap()`, or a new element added by
922
+ `push_heap()`, in 𝑂(log N) time.
 
 
 
 
 
 
 
923
 
924
  These properties make heaps useful as priority queues.
925
 
926
  `make_heap()`
927
 
 
937
  template<class RandomAccessIterator, class Compare>
938
  void push_heap(RandomAccessIterator first, RandomAccessIterator last,
939
  Compare comp);
940
  ```
941
 
 
 
 
942
  *Requires:* The range \[`first`, `last - 1`) shall be a valid heap. The
943
  type of `*first` shall satisfy the `MoveConstructible` requirements
944
+ (Table  [[tab:moveconstructible]]) and the `MoveAssignable` requirements
945
+ (Table  [[tab:moveassignable]]).
946
 
947
+ *Effects:* Places the value in the location `last - 1` into the
948
+ resulting heap \[`first`, `last`).
949
+
950
+ *Complexity:* At most log(`last - first`) comparisons.
951
 
952
  #### `pop_heap` <a id="pop.heap">[[pop.heap]]</a>
953
 
954
  ``` cpp
955
  template<class RandomAccessIterator>
 
962
 
963
  *Requires:* The range \[`first`, `last`) shall be a valid non-empty
964
  heap. `RandomAccessIterator` shall satisfy the requirements of
965
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
966
  shall satisfy the requirements of `MoveConstructible`
967
+ (Table  [[tab:moveconstructible]]) and of `MoveAssignable`
968
+ (Table  [[tab:moveassignable]]).
969
 
970
  *Effects:* Swaps the value in the location `first` with the value in the
971
  location `last - 1` and makes \[`first`, `last - 1`) into a heap.
972
 
973
+ *Complexity:* At most 2 log(`last - first`) comparisons.
974
 
975
  #### `make_heap` <a id="make.heap">[[make.heap]]</a>
976
 
977
  ``` cpp
978
  template<class RandomAccessIterator>
 
981
  template<class RandomAccessIterator, class Compare>
982
  void make_heap(RandomAccessIterator first, RandomAccessIterator last,
983
  Compare comp);
984
  ```
985
 
 
 
986
  *Requires:* The type of `*first` shall satisfy the `MoveConstructible`
987
+ requirements (Table  [[tab:moveconstructible]]) and the `MoveAssignable`
988
+ requirements (Table  [[tab:moveassignable]]).
989
 
990
+ *Effects:* Constructs a heap out of the range \[`first`, `last`).
991
+
992
+ *Complexity:* At most 3(`last - first`) comparisons.
993
 
994
  #### `sort_heap` <a id="sort.heap">[[sort.heap]]</a>
995
 
996
  ``` cpp
997
  template<class RandomAccessIterator>
 
1000
  template<class RandomAccessIterator, class Compare>
1001
  void sort_heap(RandomAccessIterator first, RandomAccessIterator last,
1002
  Compare comp);
1003
  ```
1004
 
 
 
1005
  *Requires:* The range \[`first`, `last`) shall be a valid heap.
1006
  `RandomAccessIterator` shall satisfy the requirements of
1007
  `ValueSwappable` ([[swappable.requirements]]). The type of `*first`
1008
  shall satisfy the requirements of `MoveConstructible`
1009
+ (Table  [[tab:moveconstructible]]) and of `MoveAssignable`
1010
+ (Table  [[tab:moveassignable]]).
1011
 
1012
+ *Effects:* Sorts elements in the heap \[`first`, `last`).
1013
+
1014
+ *Complexity:* At most N log N comparisons, where N = `last - first`.
1015
 
1016
  #### `is_heap` <a id="is.heap">[[is.heap]]</a>
1017
 
1018
  ``` cpp
1019
  template<class RandomAccessIterator>
1020
  bool is_heap(RandomAccessIterator first, RandomAccessIterator last);
1021
  ```
1022
 
1023
  *Returns:* `is_heap_until(first, last) == last`
1024
 
1025
+ ``` cpp
1026
+ template<class ExecutionPolicy, class RandomAccessIterator>
1027
+ bool is_heap(ExecutionPolicy&& exec,
1028
+ RandomAccessIterator first, RandomAccessIterator last);
1029
+ ```
1030
+
1031
+ *Returns:*
1032
+ `is_heap_until(std::forward<ExecutionPolicy>(exec), first, last) == last`
1033
+
1034
  ``` cpp
1035
  template<class RandomAccessIterator, class Compare>
1036
  bool is_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
1037
  ```
1038
 
1039
  *Returns:* `is_heap_until(first, last, comp) == last`
1040
 
1041
+ ``` cpp
1042
+ template<class ExecutionPolicy, class RandomAccessIterator, class Compare>
1043
+ bool is_heap(ExecutionPolicy&& exec,
1044
+ RandomAccessIterator first, RandomAccessIterator last, Compare comp);
1045
+ ```
1046
+
1047
+ *Returns:*
1048
+
1049
+ ``` cpp
1050
+ is_heap_until(std::forward<ExecutionPolicy>(exec), first, last, comp) == last
1051
+ ```
1052
+
1053
  ``` cpp
1054
  template<class RandomAccessIterator>
1055
  RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last);
1056
+ template<class ExecutionPolicy, class RandomAccessIterator>
1057
+ RandomAccessIterator is_heap_until(ExecutionPolicy&& exec,
1058
+ RandomAccessIterator first, RandomAccessIterator last);
1059
+
1060
  template<class RandomAccessIterator, class Compare>
1061
  RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last,
1062
  Compare comp);
1063
+ template<class ExecutionPolicy, class RandomAccessIterator, class Compare>
1064
+ RandomAccessIterator is_heap_until(ExecutionPolicy&& exec,
1065
+ RandomAccessIterator first, RandomAccessIterator last,
1066
+ Compare comp);
1067
  ```
1068
 
1069
+ *Returns:* If `(last - first) < 2`, returns `last`. Otherwise, returns
1070
+ the last iterator `i` in \[`first`, `last`\] for which the range
1071
  \[`first`, `i`) is a heap.
1072
 
1073
  *Complexity:* Linear.
1074
 
1075
  ### Minimum and maximum <a id="alg.min.max">[[alg.min.max]]</a>
 
1078
  template<class T> constexpr const T& min(const T& a, const T& b);
1079
  template<class T, class Compare>
1080
  constexpr const T& min(const T& a, const T& b, Compare comp);
1081
  ```
1082
 
1083
+ *Requires:* For the first form, type `T` shall be `LessThanComparable`
1084
+ (Table  [[tab:lessthancomparable]]).
1085
 
1086
  *Returns:* The smaller value.
1087
 
1088
  *Remarks:* Returns the first argument when the arguments are equivalent.
1089
 
1090
+ *Complexity:* Exactly one comparison.
1091
+
1092
  ``` cpp
1093
  template<class T>
1094
  constexpr T min(initializer_list<T> t);
1095
  template<class T, class Compare>
1096
  constexpr T min(initializer_list<T> t, Compare comp);
1097
  ```
1098
 
1099
+ *Requires:* `T` shall be `CopyConstructible` and `t.size() > 0`. For the
1100
+ first form, type `T` shall be `LessThanComparable`.
1101
 
1102
  *Returns:* The smallest value in the initializer_list.
1103
 
1104
  *Remarks:* Returns a copy of the leftmost argument when several
1105
  arguments are equivalent to the smallest. 
1106
 
1107
+ *Complexity:* Exactly `t.size() - 1` comparisons.
1108
+
1109
  ``` cpp
1110
  template<class T> constexpr const T& max(const T& a, const T& b);
1111
  template<class T, class Compare>
1112
  constexpr const T& max(const T& a, const T& b, Compare comp);
1113
  ```
1114
 
1115
+ *Requires:* For the first form, type `T` shall be `LessThanComparable`
1116
+ (Table  [[tab:lessthancomparable]]).
1117
 
1118
  *Returns:* The larger value.
1119
 
1120
  *Remarks:* Returns the first argument when the arguments are equivalent.
1121
 
1122
+ *Complexity:* Exactly one comparison.
1123
+
1124
  ``` cpp
1125
  template<class T>
1126
  constexpr T max(initializer_list<T> t);
1127
  template<class T, class Compare>
1128
  constexpr T max(initializer_list<T> t, Compare comp);
1129
  ```
1130
 
1131
+ *Requires:* `T` shall be `CopyConstructible` and `t.size() > 0`. For the
1132
+ first form, type `T` shall be `LessThanComparable`.
1133
 
1134
  *Returns:* The largest value in the initializer_list.
1135
 
1136
  *Remarks:* Returns a copy of the leftmost argument when several
1137
  arguments are equivalent to the largest.
1138
 
1139
+ *Complexity:* Exactly `t.size() - 1` comparisons.
1140
+
1141
  ``` cpp
1142
  template<class T> constexpr pair<const T&, const T&> minmax(const T& a, const T& b);
1143
  template<class T, class Compare>
1144
  constexpr pair<const T&, const T&> minmax(const T& a, const T& b, Compare comp);
1145
  ```
1146
 
1147
+ *Requires:* For the first form, type `T` shall be `LessThanComparable`
1148
+ (Table  [[tab:lessthancomparable]]).
1149
 
1150
  *Returns:* `pair<const T&, const T&>(b, a)` if `b` is smaller than `a`,
1151
  and `pair<const T&, const T&>(a, b)` otherwise.
1152
 
1153
  *Remarks:* Returns `pair<const T&, const T&>(a, b)` when the arguments
 
1160
  constexpr pair<T, T> minmax(initializer_list<T> t);
1161
  template<class T, class Compare>
1162
  constexpr pair<T, T> minmax(initializer_list<T> t, Compare comp);
1163
  ```
1164
 
1165
+ *Requires:* `T` shall be `CopyConstructible` and `t.size() > 0`. For the
1166
+ first form, type `T` shall be `LessThanComparable`.
1167
 
1168
  *Returns:* `pair<T, T>(x, y)`, where `x` has the smallest and `y` has
1169
  the largest value in the initializer list.
1170
 
1171
  *Remarks:* `x` is a copy of the leftmost argument when several arguments
1172
  are equivalent to the smallest. `y` is a copy of the rightmost argument
1173
  when several arguments are equivalent to the largest.
1174
 
1175
+ *Complexity:* At most (3/2)`t.size()` applications of the corresponding
1176
+ predicate.
1177
 
1178
  ``` cpp
1179
  template<class ForwardIterator>
1180
+ constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
1181
+
1182
+ template<class ExecutionPolicy, class ForwardIterator>
1183
+ ForwardIterator min_element(ExecutionPolicy&& exec,
1184
+ ForwardIterator first, ForwardIterator last);
1185
 
1186
  template<class ForwardIterator, class Compare>
1187
+ constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
1188
+ Compare comp);
1189
+ template<class ExecutionPolicy, class ForwardIterator, class Compare>
1190
+ ForwardIterator min_element(ExecutionPolicy&& exec,
1191
+ ForwardIterator first, ForwardIterator last,
1192
  Compare comp);
1193
  ```
1194
 
1195
  *Returns:* The first iterator `i` in the range \[`first`, `last`) such
1196
  that for every iterator `j` in the range \[`first`, `last`) the
1197
  following corresponding conditions hold: `!(*j < *i)` or
1198
  `comp(*j, *i) == false`. Returns `last` if `first == last`.
1199
 
1200
+ *Complexity:* Exactly max(`last - first - 1`, 0) applications of the
1201
  corresponding comparisons.
1202
 
1203
  ``` cpp
1204
  template<class ForwardIterator>
1205
+ constexpr ForwardIterator max_element(ForwardIterator first, ForwardIterator last);
1206
+ template<class ExecutionPolicy, class ForwardIterator>
1207
+ ForwardIterator max_element(ExecutionPolicy&& exec,
1208
+ ForwardIterator first, ForwardIterator last);
1209
+
1210
  template<class ForwardIterator, class Compare>
1211
+ constexpr ForwardIterator max_element(ForwardIterator first, ForwardIterator last,
1212
+ Compare comp);
1213
+ template<class ExecutionPolicy, class ForwardIterator, class Compare>
1214
+ ForwardIterator max_element(ExecutionPolicy&& exec,
1215
+ ForwardIterator first, ForwardIterator last,
1216
  Compare comp);
1217
  ```
1218
 
1219
  *Returns:* The first iterator `i` in the range \[`first`, `last`) such
1220
  that for every iterator `j` in the range \[`first`, `last`) the
1221
  following corresponding conditions hold: `!(*i < *j)` or
1222
  `comp(*i, *j) == false`. Returns `last` if `first == last`.
1223
 
1224
+ *Complexity:* Exactly max(`last - first - 1`, 0) applications of the
1225
  corresponding comparisons.
1226
 
1227
  ``` cpp
1228
  template<class ForwardIterator>
1229
+ constexpr pair<ForwardIterator, ForwardIterator>
1230
  minmax_element(ForwardIterator first, ForwardIterator last);
1231
+ template<class ExecutionPolicy, class ForwardIterator>
1232
  pair<ForwardIterator, ForwardIterator>
1233
+ minmax_element(ExecutionPolicy&& exec,
1234
+ ForwardIterator first, ForwardIterator last);
1235
+
1236
+ template<class ForwardIterator, class Compare>
1237
+ constexpr pair<ForwardIterator, ForwardIterator>
1238
  minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
1239
+ template<class ExecutionPolicy, class ForwardIterator, class Compare>
1240
+ pair<ForwardIterator, ForwardIterator>
1241
+ minmax_element(ExecutionPolicy&& exec,
1242
+ ForwardIterator first, ForwardIterator last, Compare comp);
1243
  ```
1244
 
1245
  *Returns:* `make_pair(first, first)` if \[`first`, `last`) is empty,
1246
  otherwise `make_pair(m, M)`, where `m` is the first iterator in
1247
  \[`first`, `last`) such that no iterator in the range refers to a
1248
+ smaller element, and where `M` is the last iterator[^5] in \[`first`,
1249
  `last`) such that no iterator in the range refers to a larger element.
1250
 
1251
+ *Complexity:* At most
1252
+ $\max(\bigl\lfloor{\frac{3}{2}} (N-1)\bigr\rfloor, 0)$ applications of
1253
+ the corresponding predicate, where N is `last - first`.
1254
+
1255
+ ### Bounded value <a id="alg.clamp">[[alg.clamp]]</a>
1256
+
1257
+ ``` cpp
1258
+ template<class T>
1259
+ constexpr const T& clamp(const T& v, const T& lo, const T& hi);
1260
+ template<class T, class Compare>
1261
+ constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp);
1262
+ ```
1263
+
1264
+ *Requires:* The value of `lo` shall be no greater than `hi`. For the
1265
+ first form, type `T` shall be `LessThanComparable`
1266
+ (Table  [[tab:lessthancomparable]]).
1267
+
1268
+ *Returns:* `lo` if `v` is less than `lo`, `hi` if `hi` is less than `v`,
1269
+ otherwise `v`.
1270
+
1271
+ [*Note 1*: If NaN is avoided, `T` can be a floating-point
1272
+ type. — *end note*]
1273
+
1274
+ *Complexity:* At most two comparisons.
1275
 
1276
  ### Lexicographical comparison <a id="alg.lex.comparison">[[alg.lex.comparison]]</a>
1277
 
1278
  ``` cpp
1279
  template<class InputIterator1, class InputIterator2>
1280
  bool
1281
  lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
1282
  InputIterator2 first2, InputIterator2 last2);
1283
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
1284
+ bool
1285
+ lexicographical_compare(ExecutionPolicy&& exec,
1286
+ ForwardIterator1 first1, ForwardIterator1 last1,
1287
+ ForwardIterator2 first2, ForwardIterator2 last2);
1288
 
1289
  template<class InputIterator1, class InputIterator2, class Compare>
1290
  bool
1291
  lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
1292
  InputIterator2 first2, InputIterator2 last2,
1293
  Compare comp);
1294
+ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class Compare>
1295
+ bool
1296
+ lexicographical_compare(ExecutionPolicy&& exec,
1297
+ ForwardIterator1 first1, ForwardIterator1 last1,
1298
+ ForwardIterator2 first2, ForwardIterator2 last2,
1299
+ Compare comp);
1300
  ```
1301
 
1302
  *Returns:* `true` if the sequence of elements defined by the range
1303
  \[`first1`, `last1`) is lexicographically less than the sequence of
1304
  elements defined by the range \[`first2`, `last2`) and `false`
1305
  otherwise.
1306
 
1307
+ *Complexity:* At most 2 min(`last1 - first1`, `last2 - first2`)
1308
  applications of the corresponding comparison.
1309
 
1310
  *Remarks:* If two sequences have the same number of elements and their
1311
+ corresponding elements (if any) are equivalent, then neither sequence is
1312
  lexicographically less than the other. If one sequence is a prefix of
1313
  the other, then the shorter sequence is lexicographically less than the
1314
  longer sequence. Otherwise, the lexicographical comparison of the
1315
  sequences yields the same result as the comparison of the first
1316
  corresponding pair of elements that are not equivalent.
1317
 
1318
+ [*Example 1*:
1319
+
1320
+ The following sample implementation satisfies these requirements:
1321
+
1322
  ``` cpp
1323
+ for ( ; first1 != last1 && first2 != last2 ; ++first1, (void) ++first2) {
1324
  if (*first1 < *first2) return true;
1325
  if (*first2 < *first1) return false;
1326
  }
1327
  return first1 == last1 && first2 != last2;
1328
  ```
1329
 
1330
+ *end example*]
1331
+
1332
+ [*Note 1*: An empty sequence is lexicographically less than any
1333
+ non-empty sequence, but not less than any empty sequence. — *end note*]
1334
 
1335
  ### Permutation generators <a id="alg.permutation.generators">[[alg.permutation.generators]]</a>
1336
 
1337
  ``` cpp
1338
  template<class BidirectionalIterator>
 
1342
  template<class BidirectionalIterator, class Compare>
1343
  bool next_permutation(BidirectionalIterator first,
1344
  BidirectionalIterator last, Compare comp);
1345
  ```
1346
 
1347
+ *Requires:* `BidirectionalIterator` shall satisfy the requirements of
1348
+ `ValueSwappable` ([[swappable.requirements]]).
1349
+
1350
  *Effects:* Takes a sequence defined by the range \[`first`, `last`) and
1351
  transforms it into the next permutation. The next permutation is found
1352
  by assuming that the set of all permutations is lexicographically sorted
1353
+ with respect to `operator<` or `comp`.
 
 
1354
 
1355
+ *Returns:* `true` if such a permutation exists. Otherwise, it transforms
1356
+ the sequence into the smallest permutation, that is, the ascendingly
1357
+ sorted one, and returns `false`.
1358
 
1359
  *Complexity:* At most `(last - first) / 2` swaps.
1360
 
1361
  ``` cpp
1362
  template<class BidirectionalIterator>
 
1366
  template<class BidirectionalIterator, class Compare>
1367
  bool prev_permutation(BidirectionalIterator first,
1368
  BidirectionalIterator last, Compare comp);
1369
  ```
1370
 
1371
+ *Requires:* `BidirectionalIterator` shall satisfy the requirements of
1372
+ `ValueSwappable` ([[swappable.requirements]]).
1373
+
1374
  *Effects:* Takes a sequence defined by the range \[`first`, `last`) and
1375
  transforms it into the previous permutation. The previous permutation is
1376
  found by assuming that the set of all permutations is lexicographically
1377
  sorted with respect to `operator<` or `comp`.
1378
 
1379
  *Returns:* `true` if such a permutation exists. Otherwise, it transforms
1380
  the sequence into the largest permutation, that is, the descendingly
1381
  sorted one, and returns `false`.
1382
 
 
 
 
1383
  *Complexity:* At most `(last - first) / 2` swaps.
1384