title: Binary Search Algorithm from a Sorted list
body: <p>My algorithm is suppose to tell me if <strong>'x'</strong>(which has the value 5) is in the sorted array. However, I keep getting a 0. Well since my condition states that if 'x' is not in the array show 0. Where am I going wrong?</p>
  
  <pre><code>import java.util.Arrays;
  
  public class binarySeacg {
  
  public static void main (String[]args)
  {
      int[] array = {10,7,11,5,13,8};
      exchangesort(array);
      binsearch(array,5);
      System.out.println(Arrays.toString(array));
  
  }
  
  public static void exchangesort(int[] S)
  {
      int i,j,temp;
  
      for(i=0;i&lt;S.length;i++)
          for(j=i+1;j&lt;S.length;j++)
              if(S[i]&gt;S[j])
              {
                  temp = S[i];
                  S[i] = S[j];
                  S[j] = temp;
              }
  }
  
  public static int binsearch(int[] S, int x)
  {
      int location, low, high, mid;
  
      low = 1; high = S.length;
      location = 0;
  
      while(low&lt;=high &amp;&amp; location==0)
      {
          mid =(low + high)/2;
          if(x== S[mid])
              location = mid;
          else if(x &lt; S[mid])
              high = mid -1;
          else
              low = mid + 1;
      }
      System.out.println(location);
      return location;
  }
  }
  </code></pre>