@@ -13,12 +13,12 @@ public final class IterativeBinarySearch {
1313 /**
1414 * This method implements an iterative version of binary search algorithm
1515 *
16- * @param a sorted array
17- * @param the key to search in array
16+ * @param array a sorted array
17+ * @param key the key to search in array
1818 *
1919 * @return the index of key in the array or -1 if not found
2020 */
21- public static <T extends Comparable <T >> int BS (T [] array , T key ) {
21+ public static <T extends Comparable <T >> int binarySearch (T [] array , T key ) {
2222 int l , r , k , cmp ;
2323
2424 l = 0 ;
@@ -45,15 +45,15 @@ public static void main(String[] args) {
4545 Random rand = new Random ();
4646 int base = rand .nextInt (1000 );
4747
48- Integer [] array = new Integer [0xFFFF ];
48+ Integer [] array = new Integer [65535 ];
4949 for (int i = 0 ; i < array .length ; i ++) {
5050 array [i ] = base + (i + 1 );
5151 }
5252
53- Arrays .sort (array ); //if needed
54- Integer key = base + rand .nextInt (array .length );
53+ // Arrays.sort(array); //if needed
54+ Integer key = base + rand .nextInt (array .length * 2 ); //can generate keys that aren't in array
5555
56- System .out .println (BS (array , key ));
56+ System .out .println (binarySearch (array , key ));
5757 System .out .println (Arrays .binarySearch (array , key ));
5858 }
59- }
59+ }
0 commit comments