Visualization of Binary Search
1. Introduction
Binary Search is an efficient algorithm for finding a target element in a sorted array. It
repeatedly divides the search interval in half and compares the middle element with the
target. Its time complexity is O(log n), making it faster than linear search for large datasets.
2. How Binary Search Works
The Binary Search algorithm follows these steps:
1. Set the low and high pointers to the start and end of the array.
2. Find the middle element and compare it to the target.
3. If the middle element matches, return its index.
4. If the target is smaller, repeat the search on the left half; else, on the right half.
5. Repeat until the element is found or the search space is exhausted.
3. Pseudocode
Binary Search Function:
function binarySearch(arr, target):
low = 0
high = length(arr) - 1
while low <= high:
mid = (low + high) / 2
if arr[mid] == target:
return mid
else if arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1 // Element not found
4. Visualization Images
Paste your binary search visualization images below this section.