回复: [go-nuts] How to call sort.Search in a loop

2022-10-11 Thread 'ljh' via golang-nuts
Hi community, I improved my binary search example with both sort.Search and sort.Find.  Please correct me if I got new wrongs with new example. 1.  My original question was if there are duplicate entries in sorted slice.  After I located the first dup, how can I go to the rest of them without

Re: [go-nuts] How to call sort.Search in a loop

2022-10-11 Thread K. Alex Mills
Ah, my last post had a small mistake. A small modification to upper should be all you need. The function should use >= instead of >. See below. upper := sort.Search(len(haystack), func(i int) bool { return haystack[i].name >= needle.name }) On Tue, Oct 11, 2022, 7:38 AM K. Alex Mills wro

Re: [go-nuts] How to call sort.Search in a loop

2022-10-11 Thread K. Alex Mills
sort.Search runs binary search. It's only guaranteed to work when you provide it with a function which is monotonic -- true at index i implies true at all indices greater than i. Your loop style search does not provide a monotonic function, whereas your "upper" function is monotonic. However, sinc

[go-nuts] How to call sort.Search in a loop

2022-10-11 Thread 'ljh' via golang-nuts
Hi community, I used sort.Search on a sorted slice in below code started at Line 36, and it worked. I also tried to call sort.Search in a loop, started at Line 53. It does not seem to work. How can I correct the loop version? Thanks --- package main import ( "log" "sor