Skip to content

Comment on Trying Haskell

Comments

C++ quicksort :|

    #include <algorithm>
    #include <iterator>
    #include <functional>
    using namespace std;
 
    template <typename T>
    void sort(T begin, T end) {
        if (begin != end) {
            T middle = partition (begin, end,   bind2nd(less<iterator_traits<T>::value_type>(), *begin));
            sort (begin, middle);
            sort (max(begin + 1, middle), end);
        }
    }
from wikipedia

His version is explicit, sort of a C++ translation of the C algorithm (though still a bit longer than necessary even for C). I think he meant to imply "in simple C++ without STL." The awkwardness of partition() is not quite enough to overcome the LOC savings, but it's making a good effort.

The Haskell version also refrains from importing the equivalent Data.List, which would allow us to define `more` and `less` as simply `partition (< x) xs`.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.