C++ implementation of quicksort with iterators (unstable sorting)
Two problems with the code that I see immediately:


if (std::distance(begin, end) &lt;= 2) is wrong since it leaves an array like {2,1} untouched. This means that the code can screw up every time it recurses to an array of size 2.
Iter partition(Iter begin, Iter end, const T&amp; pivot): Passing pivot as const T&amp; is a bad idea. When passed as const reference, when you perform the iter_swap, the value pointed to by begin changes and so does pivot. Hence running your code gives me a segfault but changing const T&amp; to T takes care of the segfault.


Even if these are fixed, there is something more subtle. Suppose you try to sort an already sorted array like {1,1,1}. Partitioning this will return mid = begin. So unless you make an explicit check for a sorted array you will recurse infinitely on qsort(mid,end).