You are computing the checkIndex
incorrectly.
From the documentation of binarySearch
you know that wordIndex = (-(insertion point) - 1)
. Therefore wordIndex+1 = -(insertion point)
, so after flipping the sign upi get -(wordIndex+1) = insertion point
int checkIndex = -(wordIndex+1);
Your code does the negation and addition in reverse order, so your code checks a wrong word.
Note: the word that you see at checkIndex
is the word that follows, not precedes, s
in lexicographic order. Therefore, you should rename precedingWord
variable to nextWord
.