diff --git a/src/main/java/com/thealgorithms/searches/BinarySearch.java b/src/main/java/com/thealgorithms/searches/BinarySearch.java index 22096307d144..374d698370c2 100644 --- a/src/main/java/com/thealgorithms/searches/BinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/BinarySearch.java @@ -1,25 +1,20 @@ -package com.thealgorithms.searches; - -import com.thealgorithms.devutils.searches.SearchAlgorithm; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.IntStream; /** - * Binary search is one of the most popular algorithms The algorithm finds the - * position of a target value within a sorted array + * Binary search is one of the most popular algorithms. + * The algorithm finds the position of a target value within a sorted array. * *

- * Worst-case performance O(log n) Best-case performance O(1) Average - * performance O(log n) Worst-case space complexity O(1) + * Worst-case performance O(log n) Best-case performance O(1) + * Average performance O(log n) Worst-case space complexity O(1) * * @author Varun Upadhyay (https://github.com/varunu28) * @author Podshivalov Nikita (https://github.com/nikitap492) - * @see SearchAlgorithm - * @see IterativeBinarySearch */ -class BinarySearch implements SearchAlgorithm { +public class BinarySearch { /** * @param array is an array where the element should be found @@ -27,7 +22,6 @@ class BinarySearch implements SearchAlgorithm { * @param is any comparable type * @return index of the element */ - @Override public > int find(T[] array, T key) { return search(array, key, 0, array.length - 1); } @@ -43,8 +37,9 @@ public > int find(T[] array, T key) { */ private > int search(T[] array, T key, int left, int right) { if (right < left) { - return -1; // this means that the key not found + return -1; // this means that the key was not found } + // find median int median = (left + right) >>> 1; int comp = key.compareTo(array[median]); @@ -66,7 +61,12 @@ public static void main(String[] args) { 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)]; @@ -74,9 +74,20 @@ public static void main(String[] args) { BinarySearch search = new BinarySearch(); 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. 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 + ); } } +