Skip to content

Commit efa5c20

Browse files
Removed two lines
1. Removed the SearchAlgorithm Interface and Import 2. Removed the @OverRide Annotation
1 parent e493eb2 commit efa5c20

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/main/java/com/thealgorithms/searches/BinarySearch.java

+26-15
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
1-
package com.thealgorithms.searches;
2-
3-
import com.thealgorithms.devutils.searches.SearchAlgorithm;
41
import java.util.Arrays;
52
import java.util.Random;
63
import java.util.concurrent.ThreadLocalRandom;
74
import java.util.stream.IntStream;
85

96
/**
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.
129
*
1310
* <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)
1613
*
1714
* @author Varun Upadhyay (https://github.com/varunu28)
1815
* @author Podshivalov Nikita (https://github.com/nikitap492)
19-
* @see SearchAlgorithm
20-
* @see IterativeBinarySearch
2116
*/
22-
class BinarySearch implements SearchAlgorithm {
17+
public class BinarySearch {
2318

2419
/**
2520
* @param array is an array where the element should be found
2621
* @param key is an element which should be found
2722
* @param <T> is any comparable type
2823
* @return index of the element
2924
*/
30-
@Override
3125
public <T extends Comparable<T>> int find(T[] array, T key) {
3226
return search(array, key, 0, array.length - 1);
3327
}
@@ -43,8 +37,9 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
4337
*/
4438
private <T extends Comparable<T>> int search(T[] array, T key, int left, int right) {
4539
if (right < left) {
46-
return -1; // this means that the key not found
40+
return -1; // this means that the key was not found
4741
}
42+
4843
// find median
4944
int median = (left + right) >>> 1;
5045
int comp = key.compareTo(array[median]);
@@ -66,17 +61,33 @@ public static void main(String[] args) {
6661
int size = 100;
6762
int maxElement = 100000;
6863

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);
7070

7171
// The element that should be found
7272
int shouldBeFound = integers[r.nextInt(size - 1)];
7373

7474
BinarySearch search = new BinarySearch();
7575
int atIndex = search.find(integers, shouldBeFound);
7676

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+
);
7884

7985
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+
);
8191
}
8292
}
93+

0 commit comments

Comments
 (0)