trekhleb / trekhleb/javascript-algorithms

interpolationSearch may not return the first index with the value

Aperta
#306 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
JavaScript
Stelle
197k
Fork
31k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Locate the JavaScript implementation and existing tests for interpolationSearch, then reproduce the [-1, 0, 0] case and compare its result with Array.indexOf. Add regression coverage for duplicate values and ensure the search returns the first matching index for sorted integer arrays.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
search
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.