mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
Add tests, remove main
in UpperBound
(#5679)
This commit is contained in:
@ -1,9 +1,6 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* The UpperBound method is used to return an index pointing to the first
|
||||
@ -25,28 +22,6 @@ import java.util.stream.IntStream;
|
||||
*/
|
||||
class UpperBound implements SearchAlgorithm {
|
||||
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
// Just generate data
|
||||
Random r = ThreadLocalRandom.current();
|
||||
|
||||
int size = 100;
|
||||
int maxElement = 100000;
|
||||
|
||||
Integer[] integers = IntStream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().boxed().toArray(Integer[] ::new);
|
||||
|
||||
// The element for which the upper bound is to be found
|
||||
int val = integers[r.nextInt(size - 1)] + 1;
|
||||
|
||||
UpperBound search = new UpperBound();
|
||||
int atIndex = search.find(integers, val);
|
||||
|
||||
System.out.printf("Val: %d. Upper Bound Found %d at index %d. An array length %d%n", val, integers[atIndex], atIndex, size);
|
||||
|
||||
boolean toCheck = integers[atIndex] > val || integers[size - 1] < val;
|
||||
System.out.printf("Upper Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array is an array where the UpperBound value is to be found
|
||||
* @param key is an element for which the UpperBound is to be found
|
||||
|
Reference in New Issue
Block a user