I am trying to clear up my conceptions of merge sort. What I cannot understand how merge sort takes less number of comparisons during best case.
Let me explain, looking at the merge procedure given below, I can make some inferences. First, merge sort will always take $\log_2{n}$ divisions. Whether it is best or the worst case. The depth of the tree will always be the same. Second, the number of comparisons should always be the same, since the $if$ (comparison) block is always used.
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
What I can't understand is why people say that the number of comparisons will be less for the best case and more for the worst case. Looking at the algorithm I just don't see how that is possible.
Lets take to lists:
$L_1 = \{a_1,a_2,a_3,a_4\}$
$L_2 = \{b_1,b_2,b_3,b_4\}$
Regardless of the fact that whether all elements of $L_1$ is less than $L_2$ (best case) or all elements of $L_1$ are greater than $L_2$ (worst case), the merge procedure will use the exact same number of operations to determine how to merge the lists.
Then why the difference in the number of comparisons between the best and worst case?