title: Best Time Complexity for Sorted 2D Array Search in Java
body: <p>So I have the following code to search a sorted 2D array for a target number, but I am not sure if this is the best time complexity as in terms of Big-Oh, I read that there is a way to do in O(N), but I am not sure if my algorithm is efficient enough.</p>
  
  <pre><code>public class Search2DArray {
  
  public static int search(int[][] arrayA, int number, int target) {
      int i = 0;
      int j = number - 1;
      while (i &lt; number &amp;&amp; j &gt;= 0) {
          if (arrayA[i][j] == target) {
              System.out.printf("\n Found at %d, %d", i, j);
              return 1;
          }
          if (arrayA[i][j] &gt; target) {
              j--;
          } else {
              i++;
          }
      }
  
      System.out.printf("\n Element not found");
      return 0;
  }
  
  public static void main(String[] args) {
      int arrayA[][] = {{1, 2, 3, 4}, 
                     {5, 6, 7, 8}, 
                     {9, 10, 11, 12}};
      search(arrayA, 3, 11);
  }
  }
  </code></pre>