1
- package com .thealgorithms .searches ;
2
-
3
- import com .thealgorithms .devutils .searches .SearchAlgorithm ;
4
1
import java .util .Arrays ;
5
2
import java .util .Random ;
6
3
import java .util .concurrent .ThreadLocalRandom ;
7
4
import java .util .stream .IntStream ;
8
5
9
6
/**
10
- * Binary search is one of the most popular algorithms The algorithm finds the
11
- * position of a target value within a sorted array
7
+ * Binary search is one of the most popular algorithms.
8
+ * The algorithm finds the position of a target value within a sorted array.
12
9
*
13
10
* <p>
14
- * Worst-case performance O(log n) Best-case performance O(1) Average
15
- * performance O(log n) Worst-case space complexity O(1)
11
+ * Worst-case performance O(log n) Best-case performance O(1)
12
+ * Average performance O(log n) Worst-case space complexity O(1)
16
13
*
17
14
* @author Varun Upadhyay (https://github.com/varunu28)
18
15
* @author Podshivalov Nikita (https://github.com/nikitap492)
19
- * @see SearchAlgorithm
20
- * @see IterativeBinarySearch
21
16
*/
22
- class BinarySearch implements SearchAlgorithm {
17
+ public class BinarySearch {
23
18
24
19
/**
25
20
* @param array is an array where the element should be found
26
21
* @param key is an element which should be found
27
22
* @param <T> is any comparable type
28
23
* @return index of the element
29
24
*/
30
- @ Override
31
25
public <T extends Comparable <T >> int find (T [] array , T key ) {
32
26
return search (array , key , 0 , array .length - 1 );
33
27
}
@@ -43,8 +37,9 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
43
37
*/
44
38
private <T extends Comparable <T >> int search (T [] array , T key , int left , int right ) {
45
39
if (right < left ) {
46
- return -1 ; // this means that the key not found
40
+ return -1 ; // this means that the key was not found
47
41
}
42
+
48
43
// find median
49
44
int median = (left + right ) >>> 1 ;
50
45
int comp = key .compareTo (array [median ]);
@@ -66,17 +61,33 @@ public static void main(String[] args) {
66
61
int size = 100 ;
67
62
int maxElement = 100000 ;
68
63
69
- Integer [] integers = IntStream .generate (() -> r .nextInt (maxElement )).limit (size ).sorted ().boxed ().toArray (Integer [] ::new );
64
+ Integer [] integers = IntStream
65
+ .generate (() -> r .nextInt (maxElement ))
66
+ .limit (size )
67
+ .sorted ()
68
+ .boxed ()
69
+ .toArray (Integer []::new );
70
70
71
71
// The element that should be found
72
72
int shouldBeFound = integers [r .nextInt (size - 1 )];
73
73
74
74
BinarySearch search = new BinarySearch ();
75
75
int atIndex = search .find (integers , shouldBeFound );
76
76
77
- System .out .printf ("Should be found: %d. Found %d at index %d. An array length %d%n" , shouldBeFound , integers [atIndex ], atIndex , size );
77
+ System .out .printf (
78
+ "Should be found: %d. Found %d at index %d. Array length %d%n" ,
79
+ shouldBeFound ,
80
+ integers [atIndex ],
81
+ atIndex ,
82
+ size
83
+ );
78
84
79
85
int toCheck = Arrays .binarySearch (integers , shouldBeFound );
80
- System .out .printf ("Found by system method at an index: %d. Is equal: %b%n" , toCheck , toCheck == atIndex );
86
+ System .out .printf (
87
+ "Found by system method at an index: %d. Is equal: %b%n" ,
88
+ toCheck ,
89
+ toCheck == atIndex
90
+ );
81
91
}
82
92
}
93
+
0 commit comments