mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-21 11:10:08 +08:00
@ -14,12 +14,11 @@ class ExponentialSearch 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)];
|
||||
@ -27,16 +26,12 @@ class ExponentialSearch implements SearchAlgorithm {
|
||||
ExponentialSearch search = new ExponentialSearch();
|
||||
int atIndex = search.find(integers, shouldBeFound);
|
||||
|
||||
System.out.printf(
|
||||
"Should be found: %d. Found %d at index %d. An array length %d%n",
|
||||
shouldBeFound,
|
||||
integers[atIndex],
|
||||
atIndex,
|
||||
size
|
||||
);
|
||||
System.out.printf("Should be found: %d. Found %d at index %d. An array length %d%n",
|
||||
shouldBeFound, integers[atIndex], atIndex, size);
|
||||
|
||||
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
|
||||
System.out.printf("Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
|
||||
System.out.printf(
|
||||
"Found by system method at an index: %d. Is equal: %b%n", toCheck, toCheck == atIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -54,11 +49,6 @@ class ExponentialSearch implements SearchAlgorithm {
|
||||
range = range * 2;
|
||||
}
|
||||
|
||||
return Arrays.binarySearch(
|
||||
array,
|
||||
range / 2,
|
||||
Math.min(range, array.length),
|
||||
key
|
||||
);
|
||||
return Arrays.binarySearch(array, range / 2, Math.min(range, array.length), key);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user