## Binary Search With Duplicates

### November 7, 2017

We start with a “normal” binary search, that assumes no duplicates, so we can compare later:

(define (bsearch1 lt? x xs) (let loop ((lo 0) (hi (- (vector-length xs) 1))) (let ((mid (+ lo (quotient (- hi lo) 2)))) (cond ((< hi lo) #f) ((lt? x (vector-ref xs mid)) (loop lo (- mid 1))) ((lt? (vector-ref xs mid) x) (loop (+ mid 1) hi)) (else mid)))))

Here are some examples:

> (bsearch1 < 0 '#(1 2 3 5 6 7)) #f > (bsearch1 < 1 '#(1 2 3 5 6 7)) 0 > (bsearch1 < 2 '#(1 2 3 5 6 7)) 1 > (bsearch1 < 3 '#(1 2 3 5 6 7)) 2 > (bsearch1 < 4 '#(1 2 3 5 6 7)) #f > (bsearch1 < 5 '#(1 2 3 5 6 7)) 3 > (bsearch1 < 6 '#(1 2 3 5 6 7)) 4 > (bsearch1 < 7 '#(1 2 3 5 6 7)) 5 > (bsearch1 < 8 '#(1 2 3 5 6 7)) #f

For the search with duplicates, the trick is that the “equals” branch of the multi-way comparison doesn’t end the search, it just resets the top end of the search space; the search ends when the `lo`

and `hi`

pointers cross, and is successful only if some element reset the `result`

value:

(define (bsearch2 lt? x xs) (let loop ((lo 0) (hi (- (vector-length xs) 1)) (result #f)) (let ((mid (+ lo (quotient (- hi lo) 2)))) (cond ((< hi lo) result) ((lt? x (vector-ref xs mid)) (loop lo (- mid 1) result)) ((lt? (vector-ref xs mid) x) (loop (+ mid 1) hi result)) (else (loop lo (- mid 1) mid))))))

Here are some examples:

> (define xs '#(1 2 2 3 4 4 4 4 6 6 6 6 6 6 7)) > (bsearch2 < 0 xs) #f > (bsearch2 < 1 xs) 0 > (bsearch2 < 2 xs) 1 > (bsearch2 < 3 xs) 3 > (bsearch2 < 4 xs) 4 > (bsearch2 < 5 xs) #f > (bsearch2 < 6 xs) 8 > (bsearch2 < 7 xs) 14 > (bsearch2 < 8 xs) #f

You can run the program at https://ideone.com/hyxZQi.

Advertisements

Pages: 1 2

I would just call the standard binary-search function and then linearly search backwards for the first non-matching value. Granted, if all the values are the same this is O(n), but that isn’t very likely.

@JohnCowan: In the early days of personal computing, I used a shareware database manager that used a standard binary-search function and then scanned backwards, as you suggest. I asked for the first M in a binary F/M field (female/male). You can guess what happened. I sent an email to the developer telling him how to find the first M in logarithmic rather than linear time. He thanked me, and said he had never heard of that before.

It’s probably better to skip the equality test in the loop and do a “deferred” check at the end:

bsearch returns the index of the first item greater or equal to the given value (or one past the end of the array if there is no such element). find then checks the returned position and returns it if indeed the value is at that position.

@JohnCowan, your response is consistent with your response to an earlier binary search exercise.

https://programmingpraxis.com/2016/04/29/binary-search-2/#comment-59653

Here’s a related blog post:

https://research.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html

I probably should have written:

though I think it works as it stands.

Actually, it doesn’t, integer division is the way to go.

In fact, in Python 2, int divided by int gives int and the code is OK, in Python 3, it gives a float, but indexing an array with a float gives a runtime error.

Here’s an O(log n) solution in C99.

Output:

In Python this can be easily solved with bisect_left from the bisect module. See the function named “index” in the documentation.

[…] looked at binary search in the previous exercise. Today we look at ternary search. Instead of one mid at the middle of the array, ternary search has […]

[…] looked at variants of binary search in two recent exercises. Today we look at a third […]