Optimal Selection and Scheduling of Sort Patterns