From 258fa3f648d9edcea28eefa15d26befe0f33044d Mon Sep 17 00:00:00 2001 From: Pratik Date: Sat, 2 Oct 2021 10:18:26 +0530 Subject: [PATCH 1/4] Added LowerBound search algorithm --- Searches/LowerBound.java | 97 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Searches/LowerBound.java diff --git a/Searches/LowerBound.java b/Searches/LowerBound.java new file mode 100644 index 000000000000..116d0582b6ab --- /dev/null +++ b/Searches/LowerBound.java @@ -0,0 +1,97 @@ +package Searches; + +import static java.lang.String.format; + +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 element in the range + * [first, last) which has a value not less than val, i.e. the index of the next smallest number + * just greater than or equal to that number. If there are multiple values that are equal to val it + * returns the index of the first such value. + * + *

This is an extension of BinarySearch. + * + *

Worst-case performance O(log n) Best-case performance O(1) Average performance O(log n) + * Worst-case space complexity O(1) + * + * @author Pratik Padalia (https://github.com/15pratik) + * @see SearchAlgorithm + * @see BinarySearch + */ +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.println( + format( + "Val: %d. Lower Bound Found %d at index %d. An array length %d", + val, integers[atIndex], atIndex, size)); + + boolean toCheck = integers[atIndex] >= val || integers[size - 1] < val; + System.out.println( + format( + "Lower Bound found at an index: %d. Is greater or max element: %b", 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 + * @param is any comparable type + * @return index of the LowerBound element + */ + @Override + public > int find(T[] array, T key) { + return search(array, key, 0, array.length - 1); + } + + /** + * This method implements the Generic Binary Search + * + * @param array The array to make the binary search + * @param key The number you are looking for + * @param left The lower bound + * @param right The upper bound + * @return the location of the key + */ + private > int search(T[] array, T key, int left, int right) { + if (right <= left) { + return left; + } + + // find median + int median = (left + right) >>> 1; + int comp = key.compareTo(array[median]); + + if (comp == 0) { + return median; + } else if (comp < 0) { + // median position can be a possible solution + return search(array, key, left, median); + } else { + // key we are looking is greater, so we must look on the right of median position + return search(array, key, median + 1, right); + } + } +} From 0a4c30a76f2f372a843cee9ff7bd7619585b2fa2 Mon Sep 17 00:00:00 2001 From: Pratik Date: Sun, 24 Oct 2021 10:13:15 +0530 Subject: [PATCH 2/4] Added UpperBound search algorithm --- Searches/UpperBound.java | 97 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Searches/UpperBound.java diff --git a/Searches/UpperBound.java b/Searches/UpperBound.java new file mode 100644 index 000000000000..905fdcc79557 --- /dev/null +++ b/Searches/UpperBound.java @@ -0,0 +1,97 @@ +package Searches; + +import static java.lang.String.format; + +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 element in the range + * [first, last) which has a value not less than val, i.e. the index of the next smallest number + * just greater than or equal to that number. If there are multiple values that are equal to val it + * returns the index of the first such value. + * + *

This is an extension of BinarySearch. + * + *

Worst-case performance O(log n) Best-case performance O(1) Average performance O(log n) + * Worst-case space complexity O(1) + * + * @author Pratik Padalia (https://github.com/15pratik) + * @see SearchAlgorithm + * @see BinarySearch + */ +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 lower 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.println( + format( + "Val: %d. Lower Bound Found %d at index %d. An array length %d", + val, integers[atIndex], atIndex, size)); + + boolean toCheck = integers[atIndex] >= val || integers[size - 1] < val; + System.out.println( + format( + "Lower Bound found at an index: %d. Is greater or max element: %b", 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 + * @param is any comparable type + * @return index of the LowerBound element + */ + @Override + public > int find(T[] array, T key) { + return search(array, key, 0, array.length - 1); + } + + /** + * This method implements the Generic Binary Search + * + * @param array The array to make the binary search + * @param key The number you are looking for + * @param left The lower bound + * @param right The upper bound + * @return the location of the key + */ + private > int search(T[] array, T key, int left, int right) { + if (right <= left) { + return left; + } + + // find median + int median = (left + right) >>> 1; + int comp = key.compareTo(array[median]); + + if (comp == 0) { + return median; + } else if (comp < 0) { + // median position can be a possible solution + return search(array, key, left, median); + } else { + // key we are looking is greater, so we must look on the right of median position + return search(array, key, median + 1, right); + } + } +} From 28445478e9dfd8fc8a45b4e4d43a9ac3877d3658 Mon Sep 17 00:00:00 2001 From: Pratik Date: Sun, 24 Oct 2021 10:31:34 +0530 Subject: [PATCH 3/4] fix for upper_bound --- Searches/UpperBound.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Searches/UpperBound.java b/Searches/UpperBound.java index 905fdcc79557..9be11518b2e3 100644 --- a/Searches/UpperBound.java +++ b/Searches/UpperBound.java @@ -8,9 +8,9 @@ /** * The UpperBound method is used to return an index pointing to the first element in the range - * [first, last) which has a value not less than val, i.e. the index of the next smallest number - * just greater than or equal to that number. If there are multiple values that are equal to val it - * returns the index of the first such value. + * [first, last) which has a value greater than val, or the last index if no such element exists + * i.e. the index of the next smallest number just greater than that number. If there are multiple + * values that are equal to val it returns the index of the first such value. * *

This is an extension of BinarySearch. * @@ -41,7 +41,7 @@ public static void main(String[] args) { // The element for which the lower bound is to be found int val = integers[r.nextInt(size - 1)] + 1; - UpperBound search = new UpperBound(); + LowerBound search = new LowerBound(); int atIndex = search.find(integers, val); System.out.println( @@ -84,10 +84,8 @@ private > int search(T[] array, T key, int left, int rig int median = (left + right) >>> 1; int comp = key.compareTo(array[median]); - if (comp == 0) { - return median; - } else if (comp < 0) { - // median position can be a possible solution + if (comp < 0) { + // key is smaller, median position can be a possible solution return search(array, key, left, median); } else { // key we are looking is greater, so we must look on the right of median position From 2e7b130cf0915c30d403f6b9295bd7fc160bd527 Mon Sep 17 00:00:00 2001 From: Pratik Date: Sun, 24 Oct 2021 10:34:32 +0530 Subject: [PATCH 4/4] validation fixes --- Searches/UpperBound.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Searches/UpperBound.java b/Searches/UpperBound.java index 9be11518b2e3..6f895fcafde0 100644 --- a/Searches/UpperBound.java +++ b/Searches/UpperBound.java @@ -38,28 +38,28 @@ public static void main(String[] args) { .boxed() .toArray(Integer[]::new); - // The element for which the lower bound is to be found + // The element for which the upper bound is to be found int val = integers[r.nextInt(size - 1)] + 1; - LowerBound search = new LowerBound(); + UpperBound search = new UpperBound(); int atIndex = search.find(integers, val); System.out.println( format( - "Val: %d. Lower Bound Found %d at index %d. An array length %d", + "Val: %d. Upper Bound Found %d at index %d. An array length %d", val, integers[atIndex], atIndex, size)); - boolean toCheck = integers[atIndex] >= val || integers[size - 1] < val; + boolean toCheck = integers[atIndex] > val || integers[size - 1] < val; System.out.println( format( - "Lower Bound found at an index: %d. Is greater or max element: %b", atIndex, toCheck)); + "Upper Bound found at an index: %d. Is greater or max element: %b", 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 + * @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 * @param is any comparable type - * @return index of the LowerBound element + * @return index of the UpperBound element */ @Override public > int find(T[] array, T key) {