|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4162" status="New"> |
| 5 | +<title>Worst time complexity of non-parallel versions of <tt>nth_element</tt> is underspecified</title> |
| 6 | +<section><sref ref="[alg.nth.element]"/></section> |
| 7 | +<submitter>Jiang An</submitter> |
| 8 | +<date>29 Sep 2024</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +Currently, <sref ref="[alg.nth.element]"/> doesn't specify the worst time complexity for <tt>nth_element</tt> |
| 14 | +without <tt>ExecutionPolicy</tt> parameter, which seemingly allows a complexity that is |
| 15 | +<tt>𝒪(<i>N</i><sup>2</sup>)</tt> or even worse. Presumably we should make the worst time complexity |
| 16 | +consistent between parallel and non-parallel versions. For <tt>sort</tt>, LWG <iref ref="713"/> already |
| 17 | +strengthened complexity requirements. |
| 18 | +</p> |
| 19 | +</discussion> |
| 20 | + |
| 21 | +<resolution> |
| 22 | +<p> |
| 23 | +This wording is relative to <paper num="N4988"/>. |
| 24 | +</p> |
| 25 | + |
| 26 | +<ol> |
| 27 | +<li><p>Modify <sref ref="[alg.nth.element]"/> as indicated:</p> |
| 28 | + |
| 29 | +<blockquote> |
| 30 | +<pre> |
| 31 | +template<class RandomAccessIterator> |
| 32 | + constexpr void nth_element(RandomAccessIterator first, RandomAccessIterator nth, |
| 33 | + RandomAccessIterator last); |
| 34 | +template<class ExecutionPolicy, class RandomAccessIterator> |
| 35 | + void nth_element(ExecutionPolicy&& exec, |
| 36 | + RandomAccessIterator first, RandomAccessIterator nth, |
| 37 | + RandomAccessIterator last); |
| 38 | +template<class RandomAccessIterator, class Compare> |
| 39 | + constexpr void nth_element(RandomAccessIterator first, RandomAccessIterator nth, |
| 40 | + RandomAccessIterator last, Compare comp); |
| 41 | +template<class ExecutionPolicy, class RandomAccessIterator, class Compare> |
| 42 | + void nth_element(ExecutionPolicy&& exec, |
| 43 | + RandomAccessIterator first, RandomAccessIterator nth, |
| 44 | + RandomAccessIterator last, Compare comp); |
| 45 | +template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less, |
| 46 | + class Proj = identity> |
| 47 | + requires sortable<I, Comp, Proj> |
| 48 | + constexpr I |
| 49 | + ranges::nth_element(I first, I nth, S last, Comp comp = {}, Proj proj = {}); |
| 50 | +</pre> |
| 51 | +<blockquote> |
| 52 | +<p> |
| 53 | +[…] |
| 54 | +<p/> |
| 55 | +-5- <i>Complexity</i>: <del>For the overloads with no <tt>ExecutionPolicy</tt>, linear on average. |
| 56 | +For the overloads with an <tt>ExecutionPolicy</tt>,</del> <tt>𝒪(<i>N</i>)</tt> applications |
| 57 | +of the predicate<del>,</del> and <tt>𝒪(<i>N</i> log <i>N</i>)</tt> swaps, where <tt><i>N</i> = |
| 58 | +last - first</tt>. <ins>For the overloads with no <tt>ExecutionPolicy</tt>, <tt>𝒪(<i>N</i>)</tt> |
| 59 | +on average.</ins> |
| 60 | +</p> |
| 61 | +</blockquote> |
| 62 | +</blockquote> |
| 63 | +</li> |
| 64 | +</ol> |
| 65 | +</resolution> |
| 66 | + |
| 67 | +</issue> |
0 commit comments