Implementation of peak finder I implemented this peak finder for practice. I have tested it. Everything looks pretty good to me. Please let me know if there is any improvement I can make.


  Peak Finder
  
  Let input be an array. input[i] is a peak if input[i] &gt;= input[i + 1] and input[i] &gt;= input[i - 1]. 
  
  Implement a recursive algorithm of time complexity \$O(\log{n})\$ to find a peak.


class PeakFinder &lt;E extends Comparable&lt;? super E&gt;&gt;{
  private E[] array;

  public PeakFinder(E[] array){
    if(array == null || array.length == 0)
      throw new IllegalArgumentException("array cannot be null or 0");
    this.array = array;
  }

  public E find(){
    return findUtil(0, array.length - 1);
  }

  private E findUtil(int left, int right){
      int mid = (right - left)/2 + left;
      int newLeft = mid + 1;
      int newRight = mid - 1;

      if(newLeft &lt;= right &amp;&amp; array[newLeft].compareTo(array[mid]) &gt; 0)
        return findUtil(newLeft, right);
      else if(newRight &gt;= left &amp;&amp; array[newRight].compareTo(array[mid]) &gt; 0)
        return findUtil(left, newRight);
      else
        return array[mid];
  }
}


An array always has at least one peak if it is not null or empty.

Special cases:


input[i] is a peak if input[i] &gt;= input[i+1] when input[i] is the first element in the array.
input[i] is a peak if input[i] &gt;= input[i-1] when input[i] is the last element in the array.
input[i] is a peak if input[i] is the only element in the array.
Your code is pretty good. It has elements in it which are relatively sophisticated too. For example, the recursion is clear, and the midpoint function is "right" (that needs an explanation - there's a 'well known' midpoint bug that many midpoint calculations have, but yours does not).

I prefer 1-liner code to be braced, and yours is not. That's disappointing, because I don't like reviews that focus on that only.... but that's really all I can find wrong with your code style.

There are a number of technical problems though.


why throw an exception for an empty array? Why not return null?
you have not followed the instructions correctly... it is impossible for a 0, 1, or 2 element array to actually have a peak, since there is no i - 1 or i + 1 to compare against. Additionally, there is no peak in cases like [3, 2, 2, 3], since the 2's are not peaks, and the three's are at the edges.
your first if conditions are newLeft &lt;= right and newRight &gt;= left, and those are just messy.... your findUtil should have a recursion-terminating condition if (right &lt; left) { return null; } and not spread it around in the method.
I am a pragmatist about OOP in Java... Objects are great, I agree, but there is no reason to have an instance for this code. A static method passing the input array in would be fine....:

public &lt;E extends Comparable&lt;? super E&gt;&gt; E findPeak(E[] data) { ....



Finally, about the algorithm. I am not convinced that it works... I have not run it, but I am pretty sure that the compareTo methods are the wrong way around... they should be &lt; 0 and not &gt; 0)....