描述一个 O(n log n) 时间的算法,给定一个包含 n 个整数的集合S和另一个整数x ,确定S中是否存在两个元素之和正好为 x 的元素。
我计划为此使用二进制搜索。
ALGORITHM(S,x)
S=Insertion-Sort()
for i=1 to S.length
   n=x-S[i]
   if( Binary-Search(S,n) == true)
      return { S[i],n }
Binary-Search(A, v)
low=1
high=A.length
while low ≤ high
   mid=(low+high)/2
   if v = A[mid]
     return mid
   if v > A[mid]  
      low ← mid+1
   else
      high ← mid−1
 return NIL 
我如何找到这个算法的时间复杂度?如果T(n)不是(n log n),那么正确的算法是什么?