/search

A simple binary search library, in Go, using generics

Primary LanguageGoMIT LicenseMIT

search Test Status Go Reference

A generic simple binary search library to allow finding the index of any element in an ordered slice of comparable elements

🛠 Installation

Make sure to have Go installed (Version 1.18 or higher).

Install search with go get:

$ go get -u github.com/imbue11235/search

💻 Usage

Search slices of primitive types like int, float etc.

search.Slice([]int{1, 2, 3, 4, 5, 6}, 2) // => index: 1

Search slices of custom structs

type ComparableStruct struct {
	value int
}

func (c ComparableStruct) CompareTo(other ComparableStruct) int {
	return c.value - other.value
}

element := ComparableStruct{2}
list := []ComparableStruct{{1}, {2}, {3}, {4}, {5}, {6}}

search.ComparableSlice(list, element) // => index: 1

📜 License

This project is licensed under the MIT license.