Need help with binary search
Hello all, I am currently writing a program that deals with binary searching.To give you some background on the program, a file is opened, loaded into an array.From there, the string the user enters is passed to a function that searches to see if that word exists within the file. Now, I can't seem to get it to work. It's returning false everytime the function executes.here is the code I have
Code:
int first=0;
int last =0;
word findword;
last=dictionary.size-1;
int middle=0;
int position=-1;
findword.found=false;
while(findword.found==false && first<=last)
{
middle=(first+last)/2;
if (strcmp(s,dictionary.words[middle].letters)==0)
{
findword.found=true;
position=middle;
return true;
}
else if (dictionary.words[middle].letters>s)
{
last=middle-1;
}
else
first=middle+1;
}
return false;
}