Implements the sorting model routines and algorithms, using R to call C++ for the contraction mapping