head([3]) 3 tail([3]) [ ] head([2, 0, 1]) 2 tail([2, 0, 1]) [0
Question:
head([3]) ⇒ 3
tail([3]) ⇒ [ ]
head([2, 0, 1]) ⇒ 2
tail([2, 0, 1]) ⇒ [0 , 1]
mergesort([ ]) ⇒ [ ]
mergesort([1]) ⇒ [1]
mergesort([1, 0]) ⇒ [0, 1]
mergesort([4, 3, 2, 1, 0]) ⇒ [0,1, 2, 3, 4]
1. Prove that mergesort’s division phase is correct (lines 09–21). Do not prove that the rest of mergesort is correct. Like the proofs in Cormen’s text, your proof must use a loop invariant. It must have three parts: initialization, maintenance, and termination.
1a. (10 points.) Find a loop invariant for the division loop.
1b. (10 points.) Use your loop invariant to prove the initialization part.
1c. (10 points.) Use your loop invariant to prove the maintenance part.
1d. (10 points.) Use your loop invariant to prove the termination part.
You may write your proof in English, in mathematical notation, or in a mixture of the two. Unlike the proofs in Cormen’s text, mergesort works with lists instead of arrays, so you don’t have to think about indexes. This may make your proof easier to write. Here are some hints about how to write your proof.
Think about letting U0 be the value of U when mergesort is called for the first time.
Think about the lengths of U0, U, L, and R.
Think about using multisets. A multiset is like a set, because it is an unordered collection of zero or more elements. It is unlike a set, because its elements can appear more than once. For example, {1, 2, 2, 3, 4, 4, 4 } is a multiset. It is the same multiset as { 2, 1, 4, 4, 3, 4, 2 }.
2. (10 points.) Find the run time of mergesort’s combining phase (lines 24–33). Do not find the run time for the rest of mergesort. Do not use O, Ω, or Θ. Your answer must define T(n), where n is the number of elements to be sorted.
Assume that head, tail, list concatenation ‘+’, and list creation ‘[x]’, work in constant time. (This is probably not true in Python, but assume it anyway!) Like the example in Cormen’s text, also assume that the run times of lines 24, 25 ..., 33 are constants c24, c25 ..., c33. All these constants are greater than or equal to 0.
Discrete Mathematics and Its Applications
ISBN: 978-0073383095
7th edition
Authors: Kenneth H. Rosen