Skip to content

Commit 6341199

Browse files
authored
Merge pull request TheAlgorithms#593 from Mas281/patch-1
Clean BinarySearch
2 parents 2332922 + 174b83f commit 6341199

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Searches/src/search/BinarySearch.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import java.util.Arrays;
44
import java.util.Random;
5-
import java.util.stream.Stream;
5+
import java.util.concurrent.ThreadLocalRandom
6+
import java.util.stream.IntStream;
67

78
import static java.lang.String.format;
89

@@ -70,23 +71,24 @@ private <T extends Comparable<T>> int search(T array[], T key, int left, int rig
7071

7172
// Driver Program
7273
public static void main(String[] args) {
73-
74-
//just generate data
75-
Random r = new Random();
74+
// Just generate data
75+
Random random = ThreadLocalRandom.current();
76+
7677
int size = 100;
7778
int maxElement = 100000;
78-
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[]::new);
79-
79+
80+
int[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray();
8081

81-
//the element that should be found
82-
Integer shouldBeFound = integers[r.nextInt(size - 1)];
82+
// The element that should be found
83+
int shouldBeFound = integers[r.nextInt(size - 1)];
8384

8485
BinarySearch search = new BinarySearch();
8586
int atIndex = search.find(integers, shouldBeFound);
8687

87-
System.out.println(String.format("Should be found: %d. Found %d at index %d. An array length %d"
88-
, shouldBeFound, integers[atIndex], atIndex, size));
89-
88+
System.out.println(format(
89+
"Should be found: %d. Found %d at index %d. An array length %d",
90+
shouldBeFound, integers[atIndex], atIndex, size
91+
));
9092

9193
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
9294
System.out.println(format("Found by system method at an index: %d. Is equal: %b", toCheck, toCheck == atIndex));

0 commit comments

Comments
 (0)