Implement Binary Search in Java Without Recursion: A Detailed Guide
Binary Search is one of the most efficient algorithms for searching a target element in a sorted array, boasting a time complexity of 𝑂(log 𝑛). While recursive implementations are commonly introduced in algorithms courses, non-recursive (iter...

I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
1. Introduction to Binary Search Without Recursion
1.1 Why Choose Iterative Over Recursive?
1.2 Core Idea of Iterative Binary Search
2. Implementing Binary Search in Java Without Recursion
2.1 The Code Implementation
public class BinarySearch {
public static int binarySearch(int[] array, int target) {
int low = 0;
int high = array.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2; // Prevents overflow
if (array[mid] == target) {
return mid; // Target found
} else if (array[mid] < target) {
low = mid + 1; // Narrow search to the right half
} else {
high = mid - 1; // Narrow search to the left half
}
}
return -1; // Target not found
}
public static void main(String[] args) {
int[] array = {2, 4, 6, 8, 10, 12, 14, 16};
int target = 10;
int result = binarySearch(array, target);
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found in the array.");
}
}
}
2.2 Step-by-Step Explanation of the Code
- The target is found (array[mid] == target).
- The search space is reduced to the left (high = mid - 1) or right (low = mid + 1).
3. Exploring Related Aspects of Binary Search
- Time Complexity: Each iteration halves the search space, resulting in O(logn).
- Space Complexity: Since the iterative approach does not use the call stack, the space complexity is O(1).
- Empty Array: The algorithm immediately returns -1 if the array is empty.
- Duplicates: If duplicates exist, the algorithm returns the index of the first encountered target.
- Out of Bounds: If the target is smaller than the smallest element or larger than the largest, the algorithm quickly exits.
| Aspect | Recursive | Iterative |
| Space Efficiency | Requires stack space | Constant space usage |
| Readability | Concise and elegant | Slightly verbose |
| Risk of Overflow | Possible for deep recursion | Not applicable |
- Search in a rotated sorted array.
- Find the square root of a number using approximation.
- Search in 2D matrices.
4. Practical Considerations
4.1 Extending Binary Search to Generic Types
public static <T extends Comparable<T>> int genericBinarySearch(T[] array, T target) {
int low = 0;
int high = array.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
int comparison = array[mid].compareTo(target);
if (comparison == 0) {
return mid;
} else if (comparison < 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
4.2 Binary Search in Real-World Scenarios
- Databases: Optimizing search operations in B-trees or similar data structures.
- Search Engines: Speeding up query operations.
- Competitive Programming: Frequently appearing in problems involving sorted arrays.
5. Conclusion
Read more at : Implement Binary Search in Java Without Recursion: A Detailed Guide





