public class Search {
  
  
  public static void main(String[] args) { 
    
    int[] numbers = {8, 1, 13, 64, 2, 98, 12, -3, -5};
    
    int[] sorted = {-5, -3, 1, 2, 8, 12, 13, 64, 98}; //selection sort
    
    
  }
  //if the array is not sorted, linear search is the fastest
  //possible searching algorithm 
  public static int linearSearch(int[] array, int n, int target) {
    
    for(int i=0; i<n; i++) {
      if(array[i]==target) {
        return i;
      }
    }
    return -1;
    
  }
  //if the array is in sorted order, binary search is the fastest
  //searching algorithm 
  public static int binarySearch(int[] array, int n, int target) {
    
    int low = 0;
    int high = n-1;
    
    while(low <= high) {
      int mid = (low + high)/2;
      
      if(target==array[mid]) {
        return mid;
      }else if(target < array[mid]) {
        //go to the left
        high = mid-1;
      }else{ //target > array[mid]
        //go to the right
        low = mid+1;
      }
    }
    return -1;
  }
}
