This C++ program implements the Binary Search algorithm, which efficiently searches for a target value in a sorted array. The program takes a sorted array and a target value as input, and it returns the index of the target if found, or -1 if the target is not present in the array. The algorithm operates with a time complexity of O(log n), making it suitable for large datasets. This implementation demonstrates how to use iterative binary search to locate elements efficiently.
Example Usage:
Input: arr = [1, 2, 3, 4, 5, 6, 7], target = 5
Output: Element found at index: 4
This C++ program implements the Binary Search algorithm, which efficiently searches for a target value in a sorted array. The program takes a sorted array and a target value as input, and it returns the index of the target if found, or -1 if the target is not present in the array. The algorithm operates with a time complexity of O(log n), making it suitable for large datasets. This implementation demonstrates how to use iterative binary search to locate elements efficiently.
Example Usage: Input: arr = [1, 2, 3, 4, 5, 6, 7], target = 5 Output: Element found at index: 4