Exercise 4.1-2

Write pseudocode for the brute-force method of solving the maximum-subarray problem. Your procedure should run in \(\Theta(n^2)\) time.

BRUTE-FORCE FIND-MAXIMUM-SUBARRAY(A)

01 sum = -∞

02 left = 0

03 right = 0

04 for i = 1 to A.length

05      temp-sum = 0

06      for j = i + 1 to A.length

07          temp-sum += A[j]

08          if temp-sum > sum

09              sum = temp-sum

10              left = i

11              right = j

12 return (left, right, sum)