0

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

Ajax
  • 123
  • 1
  • 7

3 Answers3

5

Without even looking at your algorithm, O(Log N) to find the minimum is forever impossible.

Because whatever the strategy, there is no way of knowing the minimum until you have seen all elements. (In an unsorted array, reading an element gives you absolutely no information on the others.)

Hence the search for the minimum is an Ω(N) problem.

1

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.

Nelfeal
  • 12,593
  • 1
  • 20
  • 39
-1

Although that Nelfeal Yves Daoust answered it well, I want to add some comment.

With only the unsorted array, there is no other way to find minimum/maximum value in sub-linear time. Since that we don't know which element is the largest or smallest, you have to look them all.

Practical Improvement

We can make it faster if we allow some more operation and space on the data structure.

  • Array is Empty

    • Insert(e) : Set minimum/maximum with the value of e. O(1)
    • Remove(e) : return some predefined value. O(1)
    • Minimum/Maximum(e) : return some predefined value. O(1)
  • Array is Not-Empty

    • Insert(e) : Compare the value of e with the minimum/maximum. If the condition is met, then set minimum/maximum with the value of e. O(1)
    • Remove(e) : If mininum/maximum == e , then set minimum/maximum as unknown (may be NULL?). Else, just remove e from the array. O(1)
    • Minimum/Maximum(e) : If the minimum/maximum is unknown, find and set the minimum/maximum using O(N) algorithm. If the minimum/maximum is known, just return it. O(N) or O(1).

Pros and Cons

Ratio of operations between Insert/Remove and Minimum/Maximum determines the performance of this algorithm. If the number of Minimum/Maximum operations are bigger than Insert/Remove, probabilistically, this algorithm works faster as the array gets bigger and bigger.

paganinist
  • 150
  • 1
  • 9
  • You are trying to solve a different problem, which is the dynamic maintenance of the min/max. It is wrong that Remove(e) can be done in O(1) time. –  Sep 08 '21 at 16:13