Sorted mismatch algorithm