Skip to main content
edited tags
Link
Raphael
  • 73.4k
  • 31
  • 184
  • 406
Post Closed as "Duplicate" by Raphael
Source Link
Sultan
  • 21
  • 1
  • 2

What is the time complexity of binary sum (recursion)

Currently, I'm reading Data Structures and Algorithms in Java, 6 edition by Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser link, particularly a chapter about recursions.

There is an example of algorithm which uses a recursion to calculate sum of all elements of the array:

public static int binarySum(int[ ] data, int low, int high) { if (low > high) // zero elements in subarray return 0; else if (low == high) // one element in subarray return data[low]; else { int mid = (low + high) / 2; return binarySum(data, low, mid) + binarySum(data, mid+1, high); } } 

It further says:

the running time of binarySum is O(n), as there are 2n−1 method calls, each requiring constant time.

I don't understand why it is that the time complexity is O(n) and not O(logn). What am I missing here?