Created
May 31, 2024 02:19
-
-
Save mikedll/cf4e81efb34336c4f07eb3d87dfe56ad to your computer and use it in GitHub Desktop.
Binary Search to find an Insertion Index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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