Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with this inductive proof that mergesort is O(n)?

Comparison based sorting is big omega of nlog(n), so we know that mergesort can't be O(n). Nevertheless, I can't find the problem with the following proof:

Proposition P(n): For a list of length n, mergesort takes O(n) time.

P(0): merge sort on the empty list just returns the empty list.

Strong induction: Assume P(1), ..., P(n-1) and try to prove P(n). We know that at each step in a recursive mergesort, two approximately "half-lists" are mergesorted and then "zipped up". The mergesorting of each half list takes, by induction, O(n/2) time. The zipping up takes O(n) time. So the algorithm has a recurrence relation of M(n) = 2M(n/2) + O(n) which is 2O(n/2) + O(n) which is O(n).

like image 841
rjkaplan Avatar asked Dec 13 '22 08:12

rjkaplan


1 Answers

Compare the "proof" that linear search is O(1).

  1. Linear search on an empty array is O(1).
  2. Linear search on a nonempty array compares the first element (O(1)) and then searches the rest of the array (O(1)). O(1) + O(1) = O(1).

The problem here is that, for the induction to work, there must be one big-O constant that works both for the hypothesis and the conclusion. That's impossible here and impossible for your proof.

like image 75
Per Avatar answered Dec 25 '22 22:12

Per