Skip to content

Instantly share code, notes, and snippets.

@ryankearney
Forked from anonymous/gist:8735799
Last active August 29, 2015 13:55
Show Gist options
  • Save ryankearney/8735912 to your computer and use it in GitHub Desktop.
Save ryankearney/8735912 to your computer and use it in GitHub Desktop.
public static void BinarySearch(String Filename) throws IOException {
//int index = -1;
int lower = 0;
int upper = elementCount -1; //stateCollection.length - 1;
int current;
boolean found = false;
int probe = 0;
String searchKey;
String c;
String p;
System.out.println(" \nSearch Results for Binary Searching \n ");
BufferedReader reader = new BufferedReader(new FileReader(Filename));
System.out.printf("%-20s%-18s%-18s%-18s\n", "Arguments", "Found", "NotFound", "No. of Probes\n");
for (int x = 0; x < stateSearch.length; x++) {
while ((searchKey = reader.readLine()) != null) {
while (lower <= upper && !found) {
probe++;
current = (lower + upper) / 2;
//String value = null;
//int z = 0;
//z++;
if (stateCollection[current].GetState().equals(searchKey)) {
//index = current;
found = true;
} else {
if (stateCollection[current].GetState().compareToIgnoreCase(searchKey) < 0 ) {
lower = current + 1;
} else {
upper = current - 1;
}
}
}
if (found) {
c = "X";
p = "";
System.out.printf("%-20s%-18s%-18s%-18s\n", searchKey, c, p, probe );
} else {
c = "";
p = "X";
System.out.printf("%-20s%-18s%-18s%-18s\n", searchKey, p, c, probe);
} //end if
System.out.println();
}// end Binary
Search Results for Binary Searching
Arguments Found NotFound No. of Probes
California X 6
Texas X 6
AK X 6
California X 6
Indiana X 6
Missippi X 6
Jacksonville X 6
Okalahooma X 6
Florida X 6
Maine X 6
Hawaii X 6
Puerto_Rico X 6
FL X 6
New_York X 6
Auburn X 6
Search Results for Sequential Searching
Arguments Found NotFound No. of Probes
California X 5
Texas X 43
AK X 50
California X 5
Indiana X 14
Missippi X 50
Jacksonville X 50
Okalahooma X 50
Florida X 9
Maine X 19
Hawaii X 11
Puerto_Rico X 50
FL X 50
New_York X 32
Auburn X 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment