title: Binary search algorithm check if an integer is contained in an array
body: <p>I am having trouble implementing binary search within my code, can you explain how it works. </p>
  
  <p><code>binarySearch</code> takes an array of integers <code>a</code> and an integer <code>n</code> and uses
  binary search algorithm check if <code>n</code> is contained in <code>a</code>. it returns true if <code>n</code> is
  contained in <code>a</code>, as well as printing the number of decision made and false
  otherwise.</p>
  
  <pre><code>import java.util.Arrays;
  import java.util.Random;
  
  public class Excersice2 {
  
      public static void main(String[] args) {
          int[] arr = createArray(10);
          System.out.println("the array not sorted");
          printArray(arr);
          arr = sortArray(arr);
          System.out.println("the array sorted");
          printArray(arr);
          System.out.println(binarySearch(arr,50));
  
      }
  
      public static void printArray(int []arr)
      {
  
          System.out.println(Arrays.toString(arr));
  
      }
  
      public static int [] createArray(int n) {
          int[] arr = new int[n];
  
          Random rand = new Random();
          for(int i = 0; i &lt; n; i++) 
              arr[i] = rand.nextInt(101);
  
          return arr; 
  
      }
  
      public static int[] sortArray(int[] arr) {
          Arrays.sort(arr);
          return arr;
  
      }
  
      public static boolean binarySearch(int arr[], int n) {
          int firstIdx = 0;
          int lastIdx = - 1;
          int middle = (firstIdx + lastIdx)/2;
          int idx = arr.length/2;
  
          while( firstIdx &lt;= lastIdx) {
              if(binarySearch[middle] &lt; arr);
  
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>The outcomes should look:
  It took 2 times to find that the value 50 is contained the array. When looking through the list </p>