Created
August 19, 2013 04:30
-
-
Save vaskaloidis/6265786 to your computer and use it in GitHub Desktop.
Java Bin Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package project5; | |
public class binSearch { | |
public binSearch(String[] input, String toSearch){ | |
inp = input; | |
search = toSearch; | |
} | |
public static int search() { | |
int begin = 0; | |
int end = inp.length; | |
while(begin < end){ | |
int middle = (begin + end) / 2; | |
if(search.compareTo(inp[middle]) < 0){ | |
end = middle; | |
} else if(search.compareTo(inp[middle]) > 0){ | |
begin = middle + 1; | |
} else { | |
return middle; | |
} | |
} | |
return -(begin + 1); | |
} | |
private static String[] inp; | |
private static String search; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment