|
1 | 1 | import java.util.Scanner; |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Implements a Binary Search in Java |
5 | 4 | * |
6 | | - * @author unknown |
| 5 | + * @author Varun Upadhyay (https://github.com/varunu28) |
| 6 | + * |
7 | 7 | */ |
| 8 | + |
8 | 9 | class BinarySearch |
9 | 10 | { |
10 | | - /** |
11 | | - * This method implements the Binary Search |
12 | | - * |
13 | | - * @param array The array to make the binary search |
14 | | - * @param key The number you are looking for |
15 | | - * @param lb The lower bound |
16 | | - * @param up The upper bound |
17 | | - * @return the location of the key |
18 | | - **/ |
19 | | - public static int BS(int array[], int key, int lb, int ub) |
20 | | - { |
21 | | - if ( lb > ub) |
22 | | - return -1; |
23 | | - |
24 | | - int mid = (lb + ub) / 2; |
25 | | - |
26 | | - if (key < array[mid]) |
27 | | - return (BS(array, key, lb, mid-1)); |
28 | | - |
29 | | - if (key > array[mid]) |
30 | | - return (BS(array, key, mid + 1, ub)); |
31 | | - |
32 | | - return mid; |
33 | | - } |
34 | | - |
35 | | - |
36 | | - /** |
37 | | - * This is the main method of Binary Search |
38 | | - * |
39 | | - * @author Unknown |
40 | | - * @param args Command line parameters |
41 | | - */ |
42 | | - |
43 | | - public static void main(String[] args) |
44 | | - { |
45 | | - Scanner input=new Scanner(System.in); |
46 | | - int array[]=new int[10] ; |
47 | | - int key; |
48 | | - |
49 | | - //Input |
50 | | - System.out.println("Enter an array of 10 numbers : "); |
51 | | - |
52 | | - for (int i = 0; i < 10 ; i++ ) |
53 | | - array[i] = input.nextInt(); |
54 | | - |
55 | | - System.out.println("Enter the number to be searched : "); |
56 | | - |
57 | | - key = input.nextInt(); |
58 | | - |
59 | | - int index=BS(array, key, 0, 9); |
60 | | - |
61 | | - if (index != -1) |
62 | | - System.out.println("Number found at index number : " + index); |
63 | | - else |
64 | | - System.out.println("Not found"); |
65 | | - |
66 | | - input.close(); |
67 | | - } |
| 11 | + /** |
| 12 | + * This method implements the Generic Binary Search |
| 13 | + * |
| 14 | + * @param array The array to make the binary search |
| 15 | + * @param key The number you are looking for |
| 16 | + * @param lb The lower bound |
| 17 | + * @param ub The upper bound |
| 18 | + * @return the location of the key |
| 19 | + **/ |
| 20 | + |
| 21 | + public static <T extends Comparable<T>> int BS(T array[], T key, int lb, int ub) |
| 22 | + { |
| 23 | + if ( lb > ub) |
| 24 | + return -1; |
| 25 | + |
| 26 | + int mid = (ub+lb)/2; |
| 27 | + int comp = key.compareTo(array[mid]); |
| 28 | + |
| 29 | + if (comp < 0) |
| 30 | + return (BS(array, key, lb, mid-1)); |
| 31 | + |
| 32 | + if (comp > 0) |
| 33 | + return (BS(array, key, mid + 1, ub)); |
| 34 | + |
| 35 | + return mid; |
| 36 | + } |
| 37 | + |
| 38 | + // Driver Program |
| 39 | + public static void main(String[] args) |
| 40 | + { |
| 41 | + Scanner input=new Scanner(System.in); |
| 42 | + |
| 43 | + // For INTEGER Input |
| 44 | + Integer[] array = new Integer[10]; |
| 45 | + int key = 5; |
| 46 | + |
| 47 | + for (int i = 0; i < 10 ; i++ ) |
| 48 | + array[i] = i+1; |
| 49 | + |
| 50 | + int index = BS(array, key, 0, 9); |
| 51 | + |
| 52 | + if (index != -1) |
| 53 | + System.out.println("Number " + key + " found at index number : " + index); |
| 54 | + else |
| 55 | + System.out.println("Not found"); |
| 56 | + |
| 57 | + |
| 58 | + // For STRING Input |
| 59 | + String[] array1 = {"a", "b", "c", "d", "e"}; |
| 60 | + String key1 = "d"; |
| 61 | + |
| 62 | + int index1 = BS(array1, key1, 0, array1.length-1); |
| 63 | + |
| 64 | + if (index1 != -1) |
| 65 | + System.out.println("String " + key1 + " found at index number : " + index1); |
| 66 | + else |
| 67 | + System.out.println("Not found"); |
| 68 | + |
| 69 | + input.close(); |
| 70 | + } |
68 | 71 | } |
0 commit comments