Add tests, remove main in LowerBound (#5672)

This commit is contained in:
Hardik Pawar
2024-10-12 12:34:54 +05:30
committed by GitHub
parent 138793df1d
commit b81671e66d
3 changed files with 60 additions and 25 deletions

View File

@ -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