Bash Script for Binary Search with Example and Explanation
mid=$(( (low + high) / 2 )) and adjusts low or high until the target is found or the search space is empty.Examples
How to Think About It
Algorithm
Code
binary_search() {
local arr=(${!1})
local target=$2
local low=0
local high=$((${#arr[@]} - 1))
while (( low <= high )); do
local mid=$(((low + high) / 2))
if (( arr[mid] == target )); then
echo "Element $target found at index $mid"
return
elif (( arr[mid] < target )); then
low=$((mid + 1))
else
high=$((mid - 1))
fi
done
echo "Element $target not found in the array"
}
# Example usage
array=(1 3 5 7 9)
target=7
binary_search array[@] $targetDry Run
Let's trace searching for 7 in array (1 3 5 7 9) through the code
Initialize variables
low=0, high=4 (array length - 1)
First iteration
mid=(0+4)/2=2, arr[mid]=5, 5 < 7, so low=mid+1=3
Second iteration
mid=(3+4)/2=3, arr[mid]=7, equals target, found at index 3
| Iteration | low | high | mid | arr[mid] | Action |
|---|---|---|---|---|---|
| 1 | 0 | 4 | 2 | 5 | 5 < 7, set low=3 |
| 2 | 3 | 4 | 3 | 7 | Found target at index 3 |
Why This Works
Step 1: Divide and conquer
The script splits the array by calculating the middle index with mid=$(((low + high) / 2)) to check the middle element.
Step 2: Adjust search range
If the middle element is less than the target, it moves the low pointer up to search the right half; otherwise, it moves the high pointer down to search the left half.
Step 3: Stop condition
The loop stops when the target is found or when low exceeds high, meaning the target is not in the array.
Alternative Approaches
binary_search_recursive() {
local arr=(${!1})
local target=$2
local low=$3
local high=$4
if (( low > high )); then
echo "Element $target not found in the array"
return
fi
local mid=$(((low + high) / 2))
if (( arr[mid] == target )); then
echo "Element $target found at index $mid"
elif (( arr[mid] < target )); then
binary_search_recursive arr[@] $target $((mid + 1)) $high
else
binary_search_recursive arr[@] $target $low $((mid - 1))
fi
}
# Example usage
array=(1 3 5 7 9)
target=7
binary_search_recursive array[@] $target 0 $((${#array[@]} - 1))linear_search() {
local arr=(${!1})
local target=$2
for i in "${!arr[@]}"; do
if (( arr[i] == target )); then
echo "Element $target found at index $i"
return
fi
done
echo "Element $target not found in the array"
}
# Example usage
array=(1 3 5 7 9)
target=7
linear_search array[@] $targetComplexity: O(log n) time, O(1) space
Time Complexity
Binary search divides the search space in half each iteration, so it runs in logarithmic time relative to the array size.
Space Complexity
The iterative version uses constant extra space, only storing a few variables for indexes.
Which Approach is Fastest?
Iterative binary search is fastest and most memory efficient in Bash; recursive adds call overhead, and linear search is slower for large arrays.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative Binary Search | O(log n) | O(1) | Large sorted arrays |
| Recursive Binary Search | O(log n) | O(log n) due to call stack | Readability, small arrays |
| Linear Search | O(n) | O(1) | Unsorted or small arrays |