/Users/johnr/Dropbox/johnrayworth.info/largeFilesOutsideJSR/__IB-Other/Other/IA-Solutions-2019/Proud/Product/ComputerScienceInternalAssessmentV2/src/computerscienceinternalassessment/BinarySearch.java
 1 /*
 2  * To change this license header, choose License Headers in Project Properties.
 3  * To change this template file, choose Tools | Templates
 4  * and open the template in the editor.
 5  */
 6 package computerscienceinternalassessment;
 7 
 8 import java.util.ArrayList;
 9 
10 /**
11  *
12  * @author 14914
13  */
14 public class BinarySearch {
15     
16     public int binarySearch(ArrayList<Palette> arr, String key){
17     int low =0;
18     int high = arr.size()-1;
19     while(low <= high){               // Keep on looking for the key until the low and the high cross each other - if that does happen, it means the key was not found.
20         int mid = (low + high) / 2;
21         if(arr.get(mid).getPaletteName().equals(key))
22             return mid;               // This is what will happen if/when we find the key in the array.
23         else if(arr.get(mid).getPaletteName().compareTo(key) < 1)
24             low = mid + 1;            // since the arr[mid] value is less than the key, we can eliminate looking at the left side of the remaining elements
25         else
26             high = mid -1;            // i.e. the arr[mid] value is greater than what we are looking for, so we can eliminate looking at the right side of the remaining elements
27     }
28     return -1;
29 }
30     
31 }
32