A trainsort algorithm solution in Java