SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. func binSearch(t int, a []int) bool {
  8.     if len(a) < 2 {
  9.         return a[0] == t
  10.     }
  11.     mid := len(a) / 2
  12.     switch {
  13.     case t == a[mid]:
  14.         return true
  15.     case t < a[mid]:
  16.         return binSearch(t, a[:mid])
  17.     default:
  18.         return binSearch(t, a[mid:])
  19.     }
  20. }
  21.  
  22. func main() {
  23.     arr := []int{1,2,3,4,5,6,7,8,9,10}
  24.     target := 4
  25.     target2 := 11
  26.     if binSearch(target, arr) {
  27.         fmt.Println("pass")
  28.     }
  29.     if binSearch(target2, arr) {
  30.         fmt.Println("fail")
  31.     }
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top