Partition input into sorted portions and write to files