Write a sorting program that looks erroneous but it is actually correct
C++

Inspired by Apple's goto fail; bug.

#include &lt;vector&gt;
#include &lt;map&gt;
#include &lt;iostream&gt;

/**
 * Sorts a vector of doubles in reverse order using the bucket sort algorithm.
 */
std::vector&lt;double&gt; reverse_bucket_sort(const std::vector&lt;double&gt;&amp; input) {
    // put each element into a bucket as many times as it appears
    std::map&lt;double, int&gt; bucket_counts;
    for (auto it : input)
        ++bucket_counts[it];

    std::vector&lt;double&gt; sorted_elements; // the return value

    // loop until we are done
    while (bucket_counts.size() &gt; 0) {
        // find the largest element
        double maximum = std::numeric_limits&lt;double&gt;::lowest();
        for (auto it : bucket_counts) {
            if (it.first &gt; maximum)
                maximum = it.first;
                maximum = it.first;
        }

        // add the largest element N times to our sorted vector
        for (int i = 0; i &lt; bucket_counts[maximum]; ++i)
            sorted_elements.push_back(maximum);

        // and now erase the bucket
        bucket_counts.erase(maximum);
    }

    return sorted_elements;
}

int main(int argc, const char * argv[]) {
    std::vector&lt;double&gt; test_case = { 0, 1, 2.5, 10, 2.5, 2 };

    std::cout &lt;&lt; "unsorted:";
    for (auto it : test_case) std::cout &lt;&lt; " " &lt;&lt; it;
    std::cout &lt;&lt; std::endl;

    std::cout &lt;&lt; "sorted:";
    for (auto it : reverse_bucket_sort(test_case)) std::cout &lt;&lt; " " &lt;&lt; it;
    std::cout &lt;&lt; std::endl;

    return 0;
}



   About halfway down the page there is a bug: we have a duplicate line
 after our if check! We are always going to update the maximum with whatever
 is the last value in bucket_count.
 
 Thankfully we are OK. In C++ std::map is sorted by keys. So we are
 just reversing the buckets, which is what we want.