Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the pseudo algorithm of the Find Maximum Subarray problem that uses a divide and conquer approach in finding the contiguous subarray whose values have

Implement the pseudo algorithm of the Find Maximum Subarray problem that uses a divide and conquer approach in finding the contiguous subarray whose values have the largest sum. Given the input and output requirements, your implement should follow the pseudocode provided in the textbook/lecture. You can simulate a period of 100 days with a randomly generated price ranging from $50 to $120 and calculate daily changes in prices from the generated prices. Use a table below as a model for days, prices, and changes in price.

image text in transcribed

Inputs: stock prices for 100 days, price changes for 99 days Outputs: a sequence of 100 days prices and price changes for the days like the above table i and j indices for low and high index of the subarray with the largest sum a max profit calculated from the subarray

image text in transcribed

Day Price Change 0 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 100 113 110 85 105 102 86 63 81 101 94 106 101 79 94 90 97 13 -3 -25 20 -3 -16 -23 18 20 -7 12 -5 -22 15 -4 7 6 FIND-MAXIMUM-SUBARRAY(A, low, high) 1 if high == low 2 return (low, high, Aflow]) // base case: only one element 3 else mid = (low + high)/2] 4 (left-low, left-high, left-sum) = FIND-MAXIMUM-SUBARRAY( A, low, mid) 5 (right-low, right-high, right-sum) = FIND-MAXIMUM-SUBARRAY(A. mid + 1, high) (cross-low, cross-high, cross-sum) FIND-MAX-CROSSING-SUBARRAY (A, low, mid, high) 7 if left-sum > right-sum and left-sum > cross-sum 8 return (left-low, left-high, left-sum) 9 elseif right-sum > left-sum and right-sum > cross-sum 10 return (right-low, right-high, right-sum) 11 else return (cross-low, cross-high, cross-sum) FIND-MAX-CROSSING-SUBARRAY( A, low, mid, high) 1 left-sum = - 2 sum = 0 3 for i = mid downto low 4 sum = sum + A[i] 5 if sum > left-sum 6 left-sum 7 max-left = i 8 right-sum = - 9 sum = 0 10 fori mid + 1 to high 11 sum = sum + A[] 12 if sum > right-sum 13 right-sum = sum 14 max-right = j 15 return (max-left, max-right, left-sum + right-sum) = sum

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions