[PATCH] [libc++] Fix std::make_heap's worst case time complexity

David Majnemer david.majnemer at gmail.com
Wed Jul 9 03:14:07 PDT 2014

std::make_heap is currently implemented by iteratively applying a
sift_up-type algorithm.
Since sift-up is O(ln n), this gives std::make_heap a worst case time
complexity of O(n ln n).  Since the C++ standard mandates that
std::make_heap make no more than O(3n) comparisons, this makes our
std::make_heap out of spec.

Fix this by introducing an implementation of __sift_down and switch
std::make_heap to create the heap using it.  This gives std::make_heap
linear time complexity in the worst case.

This fixes PR20161.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140709/151953c1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PR20161.patch
Type: application/octet-stream
Size: 6492 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140709/151953c1/attachment.obj>

More information about the cfe-commits mailing list