@@ -13,12 +13,12 @@ public final class IterativeBinarySearch {
13
13
/**
14
14
* This method implements an iterative version of binary search algorithm
15
15
*
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
18
18
*
19
19
* @return the index of key in the array or -1 if not found
20
20
*/
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 ) {
22
22
int l , r , k , cmp ;
23
23
24
24
l = 0 ;
@@ -45,15 +45,15 @@ public static void main(String[] args) {
45
45
Random rand = new Random ();
46
46
int base = rand .nextInt (1000 );
47
47
48
- Integer [] array = new Integer [0xFFFF ];
48
+ Integer [] array = new Integer [65535 ];
49
49
for (int i = 0 ; i < array .length ; i ++) {
50
50
array [i ] = base + (i + 1 );
51
51
}
52
52
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
55
55
56
- System .out .println (BS (array , key ));
56
+ System .out .println (binarySearch (array , key ));
57
57
System .out .println (Arrays .binarySearch (array , key ));
58
58
}
59
- }
59
+ }
0 commit comments