Algorithm Implementation/Sorting/Patience sort

C++
Given a generic STL-style input iterator to a sequence, we shall call its elements' value type, and assume that we have some container of cards (an example could be ), and a container of piles (which, again, could be just a vector of piles).

In this case, one can define the patience sorting as follows:

For the std::lower_bound algorithm to work, a tiny utility comparator functor is needed:

Clojure
Implementation using the Patience Sort approach. The elements (newelem) put on a pile combine the "card" with a reference to the top of the previous stack, as per the algorithm. The combination is done using cons, so what gets put on a pile is a list—a descending subsequence.

Output: