Algorithm 1 Linear Time Maximum Subarray

Input: An array A[1..n]A[1..n] .

Output: A tuple (p,q,s)(p, q, s) representing the maximun subarray of AA and its summation.

1:p=1,q=1,s=A[1]p = 1, q = 1, s = A[1]

2: // A[p..q]A[p..q] is the maximum subarray and s is its summation

3:i=1,tmp=A[1]i = 1, tmp = A[1]

4: // tmp is the summation of A[i..j1]A[i..j-1]

5:for j=2j = 2 to nn do

6:if tmp<0tmp < 0 then

7:i=ji = j

8:tmp=A[j]tmp = A[j]

9:else

10:tmp=tmp+A[j]tmp = tmp + A[j]

11:end if

12:if tmp>stmp > s then

13:p=i,q=j,s=tmpp = i, q = j, s = tmp

14:end if

15:end for

16:return (p,q,r)(p, q, r)