Skip to content

Removed two lines and solved the issue in binary search algorithm in java #5529 #5530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/main/java/com/thealgorithms/searches/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
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.
*
* <p>
* 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
* @param key is an element which should be found
* @param <T> is any comparable type
* @return index of the element
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T key) {
return search(array, key, 0, array.length - 1);
}
Expand All @@ -43,8 +37,9 @@ public <T extends Comparable<T>> int find(T[] array, T key) {
*/
private <T extends Comparable<T>> 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]);
Expand All @@ -66,17 +61,33 @@ 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)];

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
);
}
}

Loading