0

When calculating the sum of array, the cost of adding the value one by one is same with the divide and conquer recursive calculation. Why this time the Divide-And-Conquer does not show its performance benefit over adding one by one?

And why the Divide-And-Conquer is much better than comparing one by one on sorting?

What's the core difference between the two case?

3
  • 2
    How would you calculate the sum of an array with divide and conquer? And O(n) is the best you can hope for this problem because you have to look at each number at least once.
    – Henry
    Commented Feb 9, 2018 at 7:00
  • 1
    The core difference is that a simple sorting algorithm is O(n^2). Each element is examined n times. So the challenge is to reduce the number of times you examine an element to something less than n. With the sum of an array, each element is examined once. There's no way to reduce once to anything less than once. Commented Feb 9, 2018 at 7:05
  • 1
    IMHO, a divide and conquer can pay of here only if it is executed in parallel (which comes with an overhead). Is your implementation using parallelization? Commented Feb 9, 2018 at 8:51

1 Answer 1

6

First of all, when calculating the sum of an array, if divide and conquer is used, the runtime recurrence will be as follows.

T(n) = 2 * T(n/2) + 1

Via the Master Theorem, this yields a runtime bound of O(n). Furthermore, while being the same runtime bound as sequential addition, this bound is optimal; the output depends on every number in the input, which cannot be read within a runtime bound smaller than O(n).

That being said, divide-and-conquer does not per se yield a better runtime bound than any other approach; it is merely a design paradigm which describes a certain approach to the problem.

Futhermore, sequential addition can also be interpreted as divide and conquer, especially if it is implemented recusively; the runtime bound would be

T(n) = T(n-1) + 1

which is also O(n).

2
  • Agree, although the DnC generally lends itself better to parallelization/multithreading Commented Feb 9, 2018 at 15:32
  • Nice answer! It's to the point, reasonably brief and complete. Upvoted. Would you consider rewriting this with a simpler vocabulary? I would like to see it accessible to a wider range of SO readers.
    – Prune
    Commented Feb 9, 2018 at 21:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.