Alex Rivera | Logout

Exactly how many comparisons does merge sort make?

Asked 2011-12-16T14:25:23.960
12

I have read that quicksort is much faster than mergesort in practice, and the reason for this is the hidden constant.

Well, the solution for the randomized quick sort complexity is 2nlnn=1.39nlogn which means that the constant in quicksort is 1.39.

But what about mergesort? What is the constant in mergesort?

Edit
Report

1 Answer

2

Merging two sorted arrays (or lists) of size k resp. m takes k+m-1 comparisons at most, min{k,m} at best. (After each comparison, we can write one value to the target, when one of the two is exhausted, no more comparisons are necessary.)

Let C(n) be the worst case number of comparisons for a mergesort of an array (a list) of n elements.

Then we have C(1) = 0, C(2) = 1, pretty obviously. Further, we have the recurrence

C(n) = C(floor(n/2)) + C(ceiling(n/2)) + (n-1)

An easy induction shows

C(n) <= n*log_2 n

On the other hand, it's easy to see that we can come arbitrarily close to the bound (for every ε > 0, we can construct cases needing more than (1-ε)*n*log_2 n comparisons), so the constant for mergesort is 1.

answered 2011-12-16T19:36:02.107

Your Answer