In: Computer Science
The following submission rules apply:
· For those questions requiring programs, the solutions must be implemented using JavaScript or Java.
o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices.
o Solutions must be provided in plain text so that formatting is not lost.
· All answers must be provided in this document.
· Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.
Design and analyze a divide-and-conquer algorithm for finding the maximum element in a list: L[0: n – 1].
Suppose we find the maximum element in both of the roughly n sized partitions of an n-element n>=1 list. Then in order to find the maximum element of the entire list we simply need to see which of the two maximum elements is the larger, and which of the two minimums is the smaller. We assume that in a1-element list the sole element is both the maximum and the minimum element.
procedure maxmin (L[0...n-1] of numbers) ->(max)
begin
if(n = = 0)
return (L[0])
else if (n = = 1)
if( L[0] < L[1])
return (L[1])
else
return (L[0])
end
Let be the number of comparisons performed by the maxmin procedure. When n = 0 clearly there are no comparisons. Thus we have T(0) = 0. Similarly, T(1) = 1. Otherwise when n > 1clearly
T(n) = 2T(n)+1
Since maxmin performs one recursive calls on partitions of roughly the total size of the list and then makes two further comparisons to sort out the max for the entire list.