Search code examples
arraysalgorithmtime-complexitydivide-and-conquer

Complexity of the following algorithm to find minimum value in an unsorted array


By using divide & conquer approach, if we divide an array into two halves repeatedly, until they reduce into size of two -after which we can in O(1) time return the minimum of the two. Extending the approach, in order to merge two subarrays A & B with their minimum 'a' & 'b' respectively, we can directly return their minimum in O(1) time -making merging step a constant time operation.

This essentially means that there are logN levels and the complexity of merging step is O(1). So does this mean that complexity of finding the minimum value in an unsorted array is O(logN) using this algorithm?

Also, refer to this discussion.

Finding the minimum in an unsorted array in logarithmic time


Solution

  • after which we can in O(1) time return the minimum of the two.

    You do compare a pair of values in constant time, but you have n/2 pairs of values to compare. That makes this first step it O(n/2) in total (which is already O(n)), and summing up every step gives O(n/2 + n/4 + n/8 + ...).

    In short, you still have to do at least n-1 comparisons. There is no loophole around that.