mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Add tests, remove main
in LowerBound
(#5672)
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 LowerBound method is used to return an index pointing to the first
|
||||
@ -25,28 +22,6 @@ import java.util.stream.IntStream;
|
||||
*/
|
||||
class LowerBound 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 lower bound is to be found
|
||||
int val = integers[r.nextInt(size - 1)] + 1;
|
||||
|
||||
LowerBound search = new LowerBound();
|
||||
int atIndex = search.find(integers, val);
|
||||
|
||||
System.out.printf("Val: %d. Lower 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("Lower Bound found at an index: %d. Is greater or max element: %b%n", atIndex, toCheck);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array is an array where the LowerBound value is to be found
|
||||
* @param key is an element for which the LowerBound is to be found
|
||||
|
Reference in New Issue
Block a user