Skip to content

Instantly share code, notes, and snippets.

@ceth-x86
Created November 15, 2020 10:20
Show Gist options
  • Select an option

  • Save ceth-x86/bd80a8e04f3ad66bb3dc9ca9aef8c057 to your computer and use it in GitHub Desktop.

Select an option

Save ceth-x86/bd80a8e04f3ad66bb3dc9ca9aef8c057 to your computer and use it in GitHub Desktop.

Revisions

  1. ceth-x86 created this gist Nov 15, 2020.
    12 changes: 12 additions & 0 deletions Binary search (stdlib).go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    func main() {
    a := []int{1, 3, 6, 10, 15, 21, 28, 36, 45, 55}
    x := 6


    i := sort.Search(len(a), func(i int) bool { return a[i] >= x })
    if i < len(a) && a[i] == x {
    fmt.Printf("found %d at index %d in %v\n", x, i, a)
    } else {
    fmt.Printf("%d not found in %v\n", x, a)
    }
    }