/Users/johnr/Desktop/IA Submissions/IA Final Submission 000307-0045 - Neil/Product/HousePointProgram_Neil/src/housepointprogram/SortingAndSearching.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 housepointprogram;
 7 
 8 /**
 9  *
10  * @author 18513
11  */
12 public class SortingAndSearching {
13     
14 
15     public int binarySearch(Student[] studentArrayList, String studentName, int counter) {
16         int low = 0;
17         int high = counter;
18         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.
19             int mid = (low + high) / 2;
20             if (studentArrayList[mid].getName().compareTo(studentName) == 0) {
21                 return mid;               // This is what will happen if/when we find the key in the array.
22             } else if (studentArrayList[mid].getName().compareTo(studentName)<0) {
23                 low = mid + 1;            // since the jobArray[mid] value is less than the key, we can eliminate looking at the left side of the remaining elements
24             } else {
25                 high = mid - 1;            // i.e. the jobArray[mid] value is greater than what we are looking for, so we can eliminate looking at the right side of the remaining elements
26             }
27         }
28         return -1;
29     }
30 
31 
32 }
33 
34 
35 
36     
37 
38