2 files changed,
26 insertions(+),
0 deletions(-)
Author:
Oleksandr Smirnov
olexsmir@gmail.com
Committed at:
2026-04-13 16:53:38 +0300
Change ID:
yutzzpslklrzzkuxmqwvtuprmzvumzlr
Parent:
cafea31
jump to
| A | algos/binary_search.go |
| A | algos/binary_search_test.go |
A
algos/binary_search.go
··· 1 +package algos 2 + 3 +func BinarySearch(arr []int, target int) int { 4 + lo, hi := 0, len(arr)-1 5 + for lo <= hi { 6 + mid := lo + (hi-lo)/2 7 + if arr[mid] == target { 8 + return mid 9 + } else if arr[mid] < target { 10 + lo = mid + 1 11 + } else { 12 + hi = mid - 1 13 + } 14 + } 15 + return -1 16 +}