Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,53 @@
import com.thealgorithms.devutils.searches.SearchAlgorithm;

/**
* Binary search is one of the most popular algorithms This class represents
* iterative version {@link BinarySearch} Iterative binary search is likely to
* have lower constant factors because it doesn't involve the overhead of
* manipulating the call stack. But in java the recursive version can be
* optimized by the compiler to this version.
* Binary search is one of the most popular algorithms.
* This class represents the iterative version of {@link BinarySearch}.
*
* <p>
* Worst-case performance O(log n) Best-case performance O(1) Average
* performance O(log n) Worst-case space complexity O(1)
* <p>Iterative binary search avoids recursion overhead and uses constant space.
*
* @author Gabriele La Greca : https://github.com/thegabriele97
* @author Podshivalov Nikita (https://github.com/nikitap492)
* <p>Performance:
* <ul>
* <li>Best-case: O(1)</li>
* <li>Average-case: O(log n)</li>
* <li>Worst-case: O(log n)</li>
* <li>Space complexity: O(1)</li>
* </ul>
*
* @author Gabriele La Greca
* @author Podshivalov Nikita
* @see SearchAlgorithm
* @see BinarySearch
*/
public final class IterativeBinarySearch implements SearchAlgorithm {

/**
* This method implements an iterative version of binary search algorithm
* Performs iterative binary search on a sorted array.
*
* @param array a sorted array
* @param key the key to search in array
* @return the index of key in the array or -1 if not found
* @param array the sorted array
* @param key the element to search
* @param <T> type of elements (must be Comparable)
* @return index of the key if found, otherwise -1
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T key) {
int l;
int r;
int k;
int cmp;
if (array == null || array.length == 0) {
return -1;
}

l = 0;
r = array.length - 1;
int left = 0;
int right = array.length - 1;

while (l <= r) {
k = (l + r) >>> 1;
cmp = key.compareTo(array[k]);
while (left <= right) {
int mid = (left + right) >>> 1;
int cmp = key.compareTo(array[mid]);

if (cmp == 0) {
return k;
return mid;
} else if (cmp < 0) {
r = --k;
right = mid - 1;
} else {
l = ++k;
left = mid + 1;
}
}

Expand Down
Loading