I think this is not a vision problem, but more an algorithm one...
Nevertheless, you could make a graph of neighborhood for each part of the image,
and use some continuity test, ie. how good is the continuity between each couple of images using for example the gradient continuity or anything else...
You probably get a big square matrix. After, you arrange the squares according to the center: the center one, is the one with good continuity for all patches (using 8 neighborhood), and put the best left continuity patch on left, the best right continuity patch on the right, ...
This solution is very "experimental", and I'm hoping you are not trying to find the best way to move patches like in the Android sample, otherwise, the task could be more "harder"...
If you find an elegant solution, please share.
Nevertheless I appreciate the help @Mathieu Barnachon gave you, please read the FAQ. It clearly states that this is not a forum that is a simple 'hey please solve my problem' service. Next time, topics like this will be closed.