Heapsort With Full-Scale Genericity I've written a heapsort implementation. It's generic: generic comparators and sequences with elements of a generic type.

The implementation (follows closely chapter 6 from Introduction to Algorithms 3rd Edition and) consists of


heapify: establishes the heap property for an element:

template&lt;typename BiDirIterator, typename Cmp&gt;
void heapify(BiDirIterator begin, BiDirIterator end,
typename std::iterator_traits&lt;BiDirIterator&gt;::difference_type i, Cmp cmp)
{
    const auto dist = std::distance(begin, end);

    while (true) {
        auto left = 2 * i + 1;
        auto right = left + 1;
        typename std::iterator_traits&lt;BiDirIterator&gt;::value_type largest;

        if (left &lt; dist &amp;&amp; cmp(begin[left], begin[i]))
            largest = left;
        else
            largest = i;
        if (right &lt; dist &amp;&amp; cmp(begin[right], begin[largest]))
            largest = right;

        if (largest == i)
            break;

        std::swap(begin[i], begin[largest]);
        i = largest;
    }
}

buildHeap: creates a heap from a permutation:

template&lt;typename BiDirIterator, typename Cmp&gt;
void buildHeap(BiDirIterator begin, BiDirIterator end, Cmp cmp)
{
    for (auto i = std::distance(begin, end) / 2 - 1; i &gt;= 1; --i)
        heapify(begin, end, i, cmp);
    heapify(begin, end, 0, cmp);
}

sort: applies Heapsort to retrieve an ordered sequence from the permutation:

template&lt;typename BiDirIterator, typename Cmp&gt;
void sort(BiDirIterator begin, BiDirIterator end, Cmp cmp)
{
    buildHeap(begin, end, cmp);
    for (auto i = std::distance(begin, end) - 1; i &gt; 0; --i) {
        std::swap(*begin, begin[i]);
        heapify(begin, std::next(begin, i - 1), 0, cmp);
    }
}



Necessary includes are:

#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;functional&gt;


In action:

#include &lt;iostream&gt;
#include &lt;array&gt;

int main()
{
    std::array&lt;int, 10&gt; perm { 5, 6, 9, 3, 6, 4, 8, 4, 87, 4 };

    heap::sort(perm.begin(), perm.end(), heap::max&lt;int&gt;{});
}


where the heap utilities are put into a separate heap namespace and heap::max is simply

template&lt;typename T&gt; using max = std::greater&lt;T&gt;;


Some questions:


Is it OK to use Cmp as a template parameter? That makes it more generic, but the error messages are way more complicated and point to stuff deep down in the implementation. (Granted, you can't say my implementation is anywhere close to "deep," but this is more of a general question.) Passing a Cmp, where Cmp is some alias for a type, would also be possible but not as generic. std::sort also uses the template parameter, though, so I do so as well.
Is my use of iterators alright? I tried to keep the interface clean with iterators for interaction with standard containers, but the implementation also uses numerical indices when it's more convenient.
heapify is ugly: there's an infinite loop with a break condition and it basically finds the largest element out of three while checking that they are within range (i.e., &lt; dist). I have a feeling this can be beautified both syntactically and semantically. Some hint on that would be appreciated.
The implementation is supposed to work with bidirectional iterators. std::list doesn't work because subscripting is not supported, it's not efficient for std::list, etc. Should I replace subscripting with std::next to make it work on std::list at the cost of performance guarantees? std::list would work, but it might be frickin' slow asymptotically.
left = 2 * i + 1; for a non-random access iterator this is an operation that is linear with regards to i so it's slow for std::list as well.

Heapify can be adust to not need to std::advance an arbitrary amount by going over the list multiple times:

template&lt;typename BiDirIterator, typename Cmp&gt;
void heapify(BiDirIterator begin, BiDirIterator end, Cmp cmp)
{
    bool changed = false;
    do{
        auto parent = begin;
        auto left = begin == end? end : begin+1;
        auto right = left == end? end : left+1;
        while (left != end) {
            auto largest = left
            if (right != end &amp;&amp; cmp(*left, *right))
                largest = right;

            if (cmp(*parent, *largest)){
                std::swap(*parent, *largest);
                changed = true;
            }

            parent = ++parent;
            left = right == end? end : right+1;
            right = left == end? end : left+1;
        }
    }while(changed);
}


This will loop over the data at most O(log n) times and puts the elements in heap order.

However for pulling out the data there is no way to only look at the affected elements in O(log n) time. Which means that you can't sort a linked list with heapsort and still be in O(n log n) time complexity. 

Instead to sort a linked list in O(n log n) time you would use merge sort. This requires that you are able to be able to split the list up into separate linked lists in that you can merge. std::list has this functionality with it's splice member function. (It also has a sort member function that will do that built-in)