all repos

scratch @ 753ee96

⭐ me doing recreational ~~drugs~~ programming
2 files changed, 26 insertions(+), 0 deletions(-)
algos: binary search
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-04-13 16:53:38 +0300
Authored at: 2026-04-10 18:16:32 +0300
Change ID: yutzzpslklrzzkuxmqwvtuprmzvumzlr
Parent: cafea31
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
        +}

      
A algos/binary_search_test.go
···
        
        1
        +package algos

      
        
        2
        +

      
        
        3
        +import "testing"

      
        
        4
        +

      
        
        5
        +func TestBinarySearch(t *testing.T) {

      
        
        6
        +	inp := []int{2, 3, 5, 6, 8, 10}

      
        
        7
        +

      
        
        8
        +	is(t, BinarySearch(inp, 420), -1)

      
        
        9
        +	is(t, BinarySearch(inp, 6), 3)

      
        
        10
        +}