title: Java Best and Worst case complexity
body: <p>What is best and worst case in this java program. I think worst case is O(n^2) and Best case O(n) or maybe O(1). Can I simply say best case in O(1) in case the list is already sorted? What if an array is empty? will it make the best case O(0)? Can anybody please explain it to me.</p>
  
  <p>Thank you in Advance</p>
  
  <pre><code>     public class playground {
        static void printSimpleMedian(int[] arr) {
      if (arr == null || arr.length == 0) {
          System.out.println("Array is empty");
      } else {
          boolean sorted = true;
          for (int i = 0; i &lt; arr.length - 1; i++) {
              if (arr[i] &gt; arr[i + 1]) {
                  sorted = false;
              }
          }
          if (!sorted) {
              for (int i = 0; i &lt; arr.length / 2; i++) {
                  int max = i;
                  int min = i;
                  for (int j = i; j &lt; arr.length - i; j++) {
                      if (arr[max] &lt; arr[j]) {
                          max = j;
  
                      }
          //System.out.print(max);
                  }
  
                  for (int j = i; j &lt; arr.length - i; j++) {
  
                      if (arr[min] &gt; arr[j]) {
                          min = j;
                      }
                      //System.out.print(min);
                  }
  
                  int tmp = arr[i];
  
                  arr[i] = arr[min];
  
                  arr[min] = tmp;
  
                  tmp = arr[arr.length - i - 1];
  
                  arr[arr.length - i - 1] = arr[max];
  
                  arr[max] = tmp;
              }
          }
  
          // if array length is even upper median is chosen
  
          System.out.println(arr[arr.length / 2]);
          }
      }
  
  
  
    }
  </code></pre>