Algorithm 2 Two Sum

Input: An array S[1..n]S[1..n] representing a set of nn integers and an integer xx .

Output: Whether there is two elements in SS whose summation equals xx .

1:MergeSort(S,1,nS, 1, n) // Θ(nlogn)\Theta(n\log n)

2:i=1i = 1

3:j=nj = n

4:while i<ji < j do

5:if S[i]+S[j]<xS[i] + S[j] < x then

6:i=i+1i = i + 1

7:else if S[i]+S[j]>xS[i] + S[j] > x then

8:j=j1j = j - 1

9:else

10:return true

11:end if

12:end while

13:return false