<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2444: Inconsistent complexity for std::sort_heap</title>
<meta property="og:title" content="Issue 2444: Inconsistent complexity for std::sort_heap">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2444.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++20">C++20</a> status.</em></p>
<h3 id="2444"><a href="lwg-defects.html#2444">2444</a>. Inconsistent complexity for <code>std::sort_heap</code></h3>
<p><b>Section:</b> 26.8.8.5 <a href="https://wg21.link/sort.heap">[sort.heap]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Fran&ccedil;ois Dumont <b>Opened:</b> 2014-10-07 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
While creating complexity tests for the GNU libstdc++ implementation <a href="https://gcc.gnu.org/ml/libstdc++/2014-10/msg00048.html">I 
stumbled</a> across a surprising requirement for the <code>std::sort_heap</code> algorithm.
<p/>
In 26.8.8.5 <a href="https://wg21.link/sort.heap">[sort.heap]</a> p3 the Standard states:
</p>
<blockquote><p>
<i>Complexity</i>: At most <code><i>N</i> log(<i>N</i>)</code> comparisons (where <code><i>N</i> == last - first</code>).
</p></blockquote>
<p>
As stated on the libstdc++ mailing list by Marc Glisse <code>sort_heap</code> can be implemented by <code><i>N</i></code> calls to 
<code>pop_heap</code>. As max number of comparisons of <code>pop_heap</code> is <code>2 * log(<i>N</i>)</code> then <code>sort_heap</code> 
max limit should be <code>2 * log(1) + 2 * log(2) + .... + 2 * log(<i>N</i>)</code> that is to say <code>2 * log(<i>N</i>!)</code>. 
In terms of <code>log(<i>N</i>)</code> we can also consider that this limit is also cap by <code>2 * <i>N</i> * log(<i>N</i>)</code> 
which is surely what the Standard wanted to set as a limit.
<p/>
This is why I would like to propose to replace paragraph 3 by:
</p>
<blockquote><p>
<i>Complexity</i>: At most <code><ins>2</ins><i>N</i> log(<i>N</i>)</code> comparisons (where <code><i>N</i> == last - first</code>).
</p></blockquote>

<p><i>[2015-02 Cologne]</i></p>

<p>
Marshall will research the maths and report back in Lenexa.
</p>

<p><i>[2015-05-06 Lenexa]</i></p>

<p>STL: I dislike exact complexity requirements, they prevent one or two extra checks in debug mode. Would it be better to say O(N log(N)) not at most?</p>

<p><i>[2017-03-04, Kona]</i></p>

<p>Move to Tentatively Ready. STL may write a paper (with Thomas &amp; Robert) offering guidance about Big-O notation vs. exact requirements.</p>


<p id="res-2444"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N3936.
</p>

<ol><li>
<p>
In 26.8.8.5 <a href="https://wg21.link/sort.heap">[sort.heap]</a> p3 the Standard states:
</p>
<blockquote>
<pre>
template&lt;class RandomAccessIterator&gt;
  void sort_heap(RandomAccessIterator first, RandomAccessIterator last);
template&lt;class RandomAccessIterator, class Compare&gt;
  void sort_heap(RandomAccessIterator first, RandomAccessIterator last,
                 Compare comp);
</pre>
<blockquote><p>
[&hellip;]
<p/>
-3- <i>Complexity</i>: At most <code><ins>2</ins><i>N</i> log(<i>N</i>)</code> comparisons (where <code><i>N</i> == last - first</code>).
</p></blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
