Go Program for Binary Search with Example and Explanation
for low <= high and adjusts low or high until the target is found or not. Example: func binarySearch(arr []int, target int) int { low, high := 0, len(arr)-1; for 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 }.Examples
How to Think About It
Algorithm
Code
package main import "fmt" func binarySearch(arr []int, target int) int { low, high := 0, len(arr)-1 for 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 } func main() { arr := []int{1, 3, 5, 7, 9} target := 5 result := binarySearch(arr, target) fmt.Println(result) }
Dry Run
Let's trace searching for 5 in [1, 3, 5, 7, 9] through the code
Initialize pointers
low = 0, high = 4 (array length - 1)
Calculate mid and compare
mid = (0 + 4) / 2 = 2, arr[mid] = 5
Check if arr[mid] equals target
arr[2] == 5, which matches target 5, return 2
| low | high | mid | arr[mid] | comparison |
|---|---|---|---|---|
| 0 | 4 | 2 | 5 | arr[mid] == target, return 2 |
Why This Works
Step 1: Divide and conquer
The code splits the array into halves by calculating the middle index with mid = (low + high) / 2.
Step 2: Compare middle element
It compares the middle element with the target using if arr[mid] == target to find a match quickly.
Step 3: Adjust search range
If the middle element is less than the target, it moves the low pointer up; otherwise, it moves the high pointer down to narrow the search.
Alternative Approaches
package main import "fmt" func binarySearchRecursive(arr []int, target, low, high int) int { if low > high { return -1 } mid := (low + high) / 2 if arr[mid] == target { return mid } else if arr[mid] < target { return binarySearchRecursive(arr, target, mid+1, high) } else { return binarySearchRecursive(arr, target, low, mid-1) } } func main() { arr := []int{1, 3, 5, 7, 9} target := 7 result := binarySearchRecursive(arr, target, 0, len(arr)-1) fmt.Println(result) }
package main import ( "fmt" "sort" ) func main() { arr := []int{1, 3, 5, 7, 9} target := 7 index := sort.Search(len(arr), func(i int) bool { return arr[i] >= target }) if index < len(arr) && arr[index] == target { fmt.Println(index) } else { fmt.Println(-1) } }
Complexity: O(log n) time, O(1) space
Time Complexity
Binary search halves the search space each step, so it runs in logarithmic time, O(log n), where n is the number of elements.
Space Complexity
The iterative version uses constant extra space O(1) because it only stores a few variables.
Which Approach is Fastest?
Iterative binary search is fastest and uses less memory than recursive. Using Go's sort.Search is convenient but adds a small overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative binary search | O(log n) | O(1) | Fastest and memory efficient |
| Recursive binary search | O(log n) | O(log n) | Clear code but uses more memory |
| Go sort.Search | O(log n) | O(1) | Quick use of built-in function |