Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BadBinarySearch {
- public static int rank(char key, char[] a) {
- int lo = 0;
- int hi = a.length - 1;
- while (lo <= hi) {
- // Key is in a[lo..hi] or not present.
- System.out.print("Lo: " + lo + "\tHi: " + hi);
- int mid = (int)((lo + ((hi - lo) / 2.0) + 0.5));
- System.out.println("\tMid: " + mid);
- if (key < a[mid]) hi = mid - 1;
- else if (key > a[mid]) lo = mid + 1;
- else return mid;
- }
- return -1;
- }
- public static void main(String[] args) {
- char[] b = {'a','b','c','d','e'};
- System.out.println("Searching for " + 'b');
- int found = rank('b',b);
- System.out.println("\nFound at " + found + "\n");
- System.out.println("Searching for " + 'd');
- found = rank('d',b);
- System.out.println("\nFound at " + found + "\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement