Optimized merge sort