Question: PLEASE I NEED HELP FOR MY ASSIGNMENT The Python function mergesort is shown below. It recursively sorts a list of numbers u using a recursive

PLEASE I NEED HELP FOR MY ASSIGNMENT

PLEASE I NEED HELP FOR MY ASSIGNMENT The Python function mergesort is

shown below. It recursively sorts a list of numbers u using a

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) 0 > [1] [0, 1] [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 vo be the value of u when mer gesort is called for the first time. Think about the lengths of U, 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 12, 1,4,4,3,4,2). 2. (10 points. Find the run time of mergesort's combining phase (lines 2433). Do not find the run time for the rest of mergesort. Do not use 0, 2, 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 TX', 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. 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) 0 > [1] [0, 1] [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 vo be the value of u when mer gesort is called for the first time. Think about the lengths of U, 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 12, 1,4,4,3,4,2). 2. (10 points. Find the run time of mergesort's combining phase (lines 2433). Do not find the run time for the rest of mergesort. Do not use 0, 2, 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 TX', 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!