Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Implement Binary Search without Recursion
To implement binary search without recursion, the program uses an iterative approach with a while loop. Binary search is an efficient algorithm that finds a specific element in a sorted list by repeatedly dividing the search space in half.
This method compares the target with the middle element of the current search range. If they match, the search is complete. If the target is smaller, it searches the left half; if larger, it searches the right half. This process continues until the element is found or the search space is exhausted.
Algorithm Steps
The iterative binary search follows these key steps ?
Initialize pointers ? Set low to 0 and high to the last index
Loop while valid range ? Continue while low ? high
Calculate middle ? Find mid = (low + high) // 2
Compare and adjust ? Update pointers based on comparison
Return result ? Return index if found, -1 otherwise
Implementation
Here's the complete implementation of iterative binary search ?
def binary_search(sorted_list, target):
low = 0
high = len(sorted_list) - 1
while low <= high:
mid = (low + high) // 2
if sorted_list[mid] == target:
return mid
elif sorted_list[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
# Test with different examples
numbers = [2, 5, 8, 12, 16, 23, 38, 45, 67, 78, 90]
print("Sorted list:", numbers)
# Search for existing element
target = 23
result = binary_search(numbers, target)
print(f"Searching for {target}: Index {result}")
# Search for non-existing element
target = 25
result = binary_search(numbers, target)
if result == -1:
print(f"Searching for {target}: Element not found")
else:
print(f"Searching for {target}: Index {result}")
Sorted list: [2, 5, 8, 12, 16, 23, 38, 45, 67, 78, 90] Searching for 23: Index 5 Searching for 25: Element not found
How It Works
Let's trace through searching for element 23 in the list [2, 5, 8, 12, 16, 23, 38, 45, 67, 78, 90] ?
Initial state ? low = 0, high = 10, mid = 5, list[5] = 23
Comparison ? 23 == 23, so return index 5
For searching element 25 (not in list) ?
Step 1 ? low = 0, high = 10, mid = 5, list[5] = 23 < 25, so low = 6
Step 2 ? low = 6, high = 10, mid = 8, list[8] = 67 > 25, so high = 7
Step 3 ? low = 6, high = 7, mid = 6, list[6] = 38 > 25, so high = 5
End ? low > high, return -1 (not found)
Multiple Search Example
def binary_search(sorted_list, target):
low = 0
high = len(sorted_list) - 1
while low <= high:
mid = (low + high) // 2
if sorted_list[mid] == target:
return mid
elif sorted_list[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
# Search multiple elements
data = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
search_elements = [1, 7, 15, 20, 3]
print("List:", data)
print("Search Results:")
for element in search_elements:
index = binary_search(data, element)
if index != -1:
print(f"Element {element} found at index {index}")
else:
print(f"Element {element} not found")
List: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] Search Results: Element 1 found at index 0 Element 7 found at index 3 Element 15 found at index 7 Element 20 not found Element 3 found at index 1
Time and Space Complexity
| Complexity Type | Iterative Binary Search | Linear Search |
|---|---|---|
| Time Complexity | O(log n) | O(n) |
| Space Complexity | O(1) | O(1) |
| Best for | Large sorted arrays | Small or unsorted arrays |
Conclusion
Iterative binary search is memory-efficient with O(1) space complexity and achieves O(log n) time complexity. It's ideal for searching in large sorted datasets where performance matters.
