Title
Inconsistent complexity for std::sort_heap
Status
c++20
Section
[sort.heap]
Submitter
François Dumont

Created on 2014-10-07.00:00:00 last changed 46 months ago

Messages

Date: 2017-03-14.03:14:09

Proposed resolution:

This wording is relative to N3936.

  1. In [sort.heap] p3 the Standard states:

    template<class RandomAccessIterator>
      void sort_heap(RandomAccessIterator first, RandomAccessIterator last);
    template<class RandomAccessIterator, class Compare>
      void sort_heap(RandomAccessIterator first, RandomAccessIterator last,
                     Compare comp);
    

    […]

    -3- Complexity: At most 2N log(N) comparisons (where N == last - first).

Date: 2017-03-15.00:00:00

[ 2017-03-04, Kona ]

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

Date: 2015-05-06.00:00:00

[ 2015-05-06 Lenexa ]

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?

Date: 2015-03-29.19:05:53

[ 2015-02 Cologne ]

Marshall will research the maths and report back in Lenexa.

Date: 2014-10-07.00:00:00

While creating complexity tests for the GNU libstdc++ implementation I stumbled across a surprising requirement for the std::sort_heap algorithm.

In [sort.heap] p3 the Standard states:

Complexity: At most N log(N) comparisons (where N == last - first).

As stated on the libstdc++ mailing list by Marc Glisse sort_heap can be implemented by N calls to pop_heap. As max number of comparisons of pop_heap is 2 * log(N) then sort_heap max limit should be 2 * log(1) + 2 * log(2) + .... + 2 * log(N) that is to say 2 * log(N!). In terms of log(N) we can also consider that this limit is also cap by 2 * N * log(N) which is surely what the Standard wanted to set as a limit.

This is why I would like to propose to replace paragraph 3 by:

Complexity: At most 2N log(N) comparisons (where N == last - first).

History
Date User Action Args
2021-02-25 10:48:01adminsetstatus: wp -> c++20
2017-07-30 20:18:47adminsetstatus: voting -> wp
2017-06-26 13:46:20adminsetstatus: ready -> voting
2017-03-14 03:14:09adminsetmessages: + msg9101
2017-03-14 03:14:09adminsetstatus: open -> ready
2015-05-22 20:19:09adminsetmessages: + msg7457
2015-03-29 19:05:53adminsetmessages: + msg7270
2015-03-29 19:05:53adminsetstatus: new -> open
2014-10-09 21:13:52adminsetmessages: + msg7148
2014-10-07 00:00:00admincreate