mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Format code with prettier (#3375)
This commit is contained in:
@@ -2,11 +2,11 @@ package com.thealgorithms.searches;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.IntStream;
|
||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||
|
||||
/**
|
||||
* Binary search is one of the most popular algorithms The algorithm finds the
|
||||
@@ -43,7 +43,12 @@ class BinarySearch implements SearchAlgorithm {
|
||||
* @param right The upper bound
|
||||
* @return the location of the key
|
||||
*/
|
||||
private <T extends Comparable<T>> int search(T array[], T key, int left, int right) {
|
||||
private <T extends Comparable<T>> int search(
|
||||
T array[],
|
||||
T key,
|
||||
int left,
|
||||
int right
|
||||
) {
|
||||
if (right < left) {
|
||||
return -1; // this means that the key not found
|
||||
}
|
||||
@@ -68,12 +73,12 @@ class BinarySearch implements SearchAlgorithm {
|
||||
int size = 100;
|
||||
int maxElement = 100000;
|
||||
|
||||
Integer[] integers
|
||||
= IntStream.generate(() -> r.nextInt(maxElement))
|
||||
.limit(size)
|
||||
.sorted()
|
||||
.boxed()
|
||||
.toArray(Integer[]::new);
|
||||
Integer[] integers = IntStream
|
||||
.generate(() -> r.nextInt(maxElement))
|
||||
.limit(size)
|
||||
.sorted()
|
||||
.boxed()
|
||||
.toArray(Integer[]::new);
|
||||
|
||||
// The element that should be found
|
||||
int shouldBeFound = integers[r.nextInt(size - 1)];
|
||||
@@ -82,13 +87,22 @@ class BinarySearch implements SearchAlgorithm {
|
||||
int atIndex = search.find(integers, shouldBeFound);
|
||||
|
||||
System.out.println(
|
||||
format(
|
||||
"Should be found: %d. Found %d at index %d. An array length %d",
|
||||
shouldBeFound, integers[atIndex], atIndex, size));
|
||||
format(
|
||||
"Should be found: %d. Found %d at index %d. An array length %d",
|
||||
shouldBeFound,
|
||||
integers[atIndex],
|
||||
atIndex,
|
||||
size
|
||||
)
|
||||
);
|
||||
|
||||
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
||||
System.out.println(
|
||||
format(
|
||||
"Found by system method at an index: %d. Is equal: %b", toCheck, toCheck == atIndex));
|
||||
format(
|
||||
"Found by system method at an index: %d. Is equal: %b",
|
||||
toCheck,
|
||||
toCheck == atIndex
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user