Use the same binary-search window as the iterative lesson, but pass lo and hi through recursive calls.

Algorithm

Basic Implementation

basic.sh
#!/usr/bin/env bash
set -euo pipefail
arr=(1 3 5 7 9 11 13)
target=11
search() {
	local lo=$1
	local hi=$2
	if [ "$lo" -gt "$hi" ]; then
		echo -1
		return
	fi
	local mid=$((lo + (hi - lo) / 2))
	if [ "${arr[mid]}" -eq "$target" ]; then
		echo "$mid"
	elif [ "${arr[mid]}" -lt "$target" ]; then
		search $((mid + 1)) "$hi"
	else
		search "$lo" $((mid - 1))
	fi
}
search 0 $((${#arr[@]} - 1))

Complexity

  • Time: O(log n)
  • Space: O(log n) call stack

Implementation notes

  • Keep the explicit control flow. Library shortcuts would hide the state changes this lesson is meant to replay.
  • The final output is intentionally small and deterministic for cross-language comparison.
execution replay The checked-in replay follows the language-neutral state table for `search-binary-recursive`.
cross-language comparison This Bash DSA version keeps the same data and final output as every other DSA book in this wave.