For this there are sorters already. 

See Sorters.h and Sorters.cc.