Skip to content

Instantly share code, notes, and snippets.

@mikedll
Created May 31, 2024 02:19
Show Gist options
  • Save mikedll/cf4e81efb34336c4f07eb3d87dfe56ad to your computer and use it in GitHub Desktop.
Save mikedll/cf4e81efb34336c4f07eb3d87dfe56ad to your computer and use it in GitHub Desktop.
Binary Search to find an Insertion Index
func (l *Log) MInsertIndex(day string) int {
lBound := 0
uBound := len(l.Measurements)
for uBound > lBound {
mid := (uBound + lBound) / 2
cmpResult := cmp.Compare(l.Measurements[mid].Day, day)
if cmpResult <= 0 && lBound == mid {
lBound += 1
} else if cmpResult <= 0 {
lBound = mid
} else if(cmpResult > 0) {
uBound = mid
}
}
return lBound
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment