UNB/ CS/ David Bremner/ teaching/ java/ BinarySearch.java
public class BinarySearch{
    private static boolean tracing=false;
    public static void setTracing(boolean flag){
        tracing=false;
    }
    public static int search(int[] a,  int value){  
        
        int low=0;
        int high=a.length-1;

        while (low<=high){
            if (tracing)
                ArrayUtil.print(a,low,high,true);
            int mid=(low+high)/2;
            if (a[mid]==value) 
                return mid;
            if (a[mid]<value)
                low=mid+1; // search right half
            else
                high=mid-1; // search left half
        }
        return -1;
   }

    public static void main(String []args){
        int[] sorted=ArrayUtil.randomIntArray(10,10);
        
        SelectionSort.sort(sorted);
        setTracing(true);
        System.out.println(search(sorted,4));
    }
}   


//