Implementación binary search

You, goalgorithmqueueDSdata structures
Back

This is the solution for the binary search problem in exercism.org

```go
package main

func SearchInts(list []int, key int) int {
	start := 0
	end := len(list) - 1

	for start <= end {
		mid := start + (end-start)/2

		if list[mid] == key {
			return mid
		}
		if list[mid] < key {
			start = mid + 1
		} else if list[mid] > key {
			end = mid - 1
		}
	}

	return -1
}

func main() {
  list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  key := 5
  index := SearchInts(list, key)
  if index != -1 {
    println("Element found at index:", index)
  } else {
    println("Element not found")
  }
}

open terminal and run the following command:

go run main.go
© Emilio Font.RSS