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.
- int mid = lo + (hi - lo) / 2;
- System.out.print("\t" + 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("\n\nFound at " + found + "\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement