Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interpolationSearch may not return the first index with the value #306

Open
dubzzz opened this issue Feb 14, 2019 · 0 comments
Open

interpolationSearch may not return the first index with the value #306

dubzzz opened this issue Feb 14, 2019 · 0 comments

Comments

@dubzzz
Copy link
Contributor

dubzzz commented Feb 14, 2019

Details:

Behaviour of interpolationSearch may be considered inconsistent.
For some arrays it will return the index of the first match - expect(interpolationSearch([1, 1], 1)).toBe(0) - while for others it returns the second or third one.

Step to reproduce: interpolationSearch([-1, 0, 0], 0))
Expected result: 1
Got: 2

How did I find it?

Thanks to property based testing framework fast-check.
The property was the following:

import fc from 'fast-check';

fc.assert(
  fc.property(
    fc.array(fc.integer()).map(arr => [...arr].sort((a, b) => a - b)),
    fc.integer(),
    (arr, i) => expect(interpolationSearch(arr, i)).toBe(arr.indexOf(i))
  )
)

Or:

for any arr - sorted array of integers - and i - integer value
interpolationSearch(arr, i) should be equivalent to arr.indexOf(i)

@dubzzz dubzzz changed the title BUG interpolationSearch may not return the first index with the value interpolationSearch may not return the first index with the value Feb 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant