First working version of the partial sorting algorithm