Go – Binary Search in Golang

Hello everyone, in this tutorial, we will show you how to write Binary Search in Golang. The example program has been tested and shared in the same post.

Binary Search in Golang
Go – Binary Search Example in Golang

Function – Binary Search in Golang


// function responsible to perform binary search
func binarySearch(numbers []int, left int, right int, item int) (int, int)  {

	if right >= left {

		mid := left + (right - left) / 2;

		if numbers[mid] == item {

			return numbers[mid], mid

		}

		if numbers[mid] > item {

			return binarySearch(numbers, left, mid-1, item)

		}

		return binarySearch(numbers, mid + 1, right, item)
	}

	return -1, -1;
}

Full Example


package main

import "fmt"

// function responsible to perform binary search
func binarySearch(numbers []int, left int, right int, item int) (int, int)  {

	if right >= left {

		mid := left + (right - left) / 2;

		if numbers[mid] == item {

			return numbers[mid], mid

		}

		if numbers[mid] > item {

			return binarySearch(numbers, left, mid-1, item)

		}

		return binarySearch(numbers, mid + 1, right, item)
	}

	return -1, -1;
}

func main() {

	numbers := []int{ 10, 20, 30, 40, 50 }

	n := len(numbers)

	item := 50

	result, index := binarySearch(numbers, 0, n - 1, item)

	if result == -1 && index == -1 {

		fmt.Println("Item is not present")

	} else {

		fmt.Println("Item", result, "is found at index", index)

	}
}

Output


Item 50 is found at index 4

References