Skip to content

A simple binary search library, in Go, using generics

License

Notifications You must be signed in to change notification settings

imbue11235/search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.