Question
NEED HELP WITH MY ASSIGNMENT. PLEASE ANSWER QUESTION 1 AND 2, Thank you. Here are some examples of how head, tail, and mergesort work, where
NEED HELP WITH MY ASSIGNMENT. PLEASE ANSWER QUESTION 1 AND 2, Thank you.
Here are some examples of how head, tail, and mergesort work, where the arrow means returns. Python source code for these functions is on the file mergesort.py if you want to try them on your own examples.
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 mergesorts division phase is correct (lines 0921). Do not prove that the rest of mergesort is correct. Like the proofs in Cormens 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 Cormens text, mergesort works with lists instead of arrays, so you dont 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 mergesorts combining phase (lines 2433). 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 Cormens text, also assume that the run times of lines 24, 25 ..., 33 are constants c, c ..., c. All these constants are greater than or equal to 0
The Python function mergesort is shown below. It recursively sorts a list of numbers u using a recursive divide-and-conquer algorithm, then returns the sorted list s. The function head returns the first element of a nonempty list Q, and the function tail returns a new list like g but without its first element. Lines 06-07 detect if u is trivially sorted. Lines 0921 divide u into two halves, L and R, of approximately equal lengths. Lines 2223 recursively sort L and R. Lines 24-33 combines the sorted lists L and R into a sorted list s. 01 def head(Q): 02 return Q[0] 03 def tail(Q): 04 return Q[1:] 05 def mergesort (U): 06 if U == [or tail(U) == [] : 07 return U 08 else: L = [] R = 0) while True: if U == ] : break else: L = L + [head (U)] U = tail(U) if U == []: break else: R = R + [head (U) ] U = tail(U) L = merge sort(L) R = merge sort(R) S = [] while L != [] and R != [] : if head (L)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started