0

I need help with binary searching arrays, my problem is that I feel like I have all the right requirements but it's not searching at all. I don't get any error messages. Just not getting the results I want. The array is globally initialized as well as the max number of words. This is what my function looks like:

int wordsFindFast(const char const* w){
/*
    ROLE            Determines whether a given word is in our words array
                    Implements fast binary search algorithm
    PARAMETERS      w   word to look for in the words array
    RETURN VALUE    1   true if the word was found
                    0   false if the word was not found
*/

int first, middle, last;

first = 0;
last = (MAX_NB_WORDS) - 1;
middle = (first+last)/2;

while(first <= last)
{
    if (words[middle] < w){
        first = middle + 1;
    }//end if
    else if(words[middle] == w){
        return 1;
    }//end else if
    else
        last = middle - 1;
        middle = (first + last)/2;
}//end while

return 0;
}
japreiss
  • 11,111
  • 2
  • 40
  • 77

2 Answers2

2

you can not compare strings in c as

if (words[middle] < w)

use strcmp() as,

while(first <= last)
{
    if (strcmp(words[middle],w)==0)
    {
         return 1;

    }
   else if(strcmp(words[middle], w)<0)
    {
        first=middle+1;
    } 
    else
        last = middle - 1;
  middle = (first + last)/2;
}//end while
LearningC
  • 3,182
  • 1
  • 12
  • 19
  • because you don't use relational operators for strings; and that's because when you do so you are comparing the address of where the strings are stored. And, that's an error. – attaboy182 Mar 20 '14 at 19:03
  • I tested this out and it worked partially. So, now my program is crashing and also, some of the words that I'm testing are in my list are not showing up true in this search. – user3443512 Mar 20 '14 at 19:20
  • @user3443512 this code looks fine. can you post the input and output for which program crashes. and make sure that input words are sorted. – LearningC Mar 20 '14 at 19:24
  • @LearningC I figured out the crashing problem. For some reason when I used strcmp it was making the program crash. So, I changed it to strcasecmp and it works just fine now. However, my new problem is with my linear search! It doesn't stop, meaning the program keeps running after finding the words in the list. This is what my linear algorithm looks like: – user3443512 Mar 20 '14 at 22:47
  • int i = 0; for (i=0; i < MAX_NB_WORDS; i++){ if (strcasecmp(w,words[i])== 0){ return 1; } } return 0; – user3443512 Mar 20 '14 at 22:53
  • @user3443512 you used [strcasecmp()](http://stackoverflow.com/questions/2512591/strcasecmp-in-c-returns-156-instead-of-0-any-ideas-why) properly. there is no problem in it – LearningC Mar 21 '14 at 04:28
0

You cannot compare string in C. You should compare them character-by-character using the standard library function strcmp declared in the header string.h. Also, note that

const char const *w

in the parameter list of the function wordsFindFast is the same as

const char *w

which means w is a pointer to an object of type char which is constant, i.e., is read only. You should change your function to -

int wordsFindFast(const char *w) {
    int first = 0;  
    int last = MAX_NB_WORDS;
    int middle;

    int cmpval;

    while(first <= last) {
        middle = (first + last) / 2
        cmpval = strcmp(w, words[middle]);
        if(cmpval == 0)
            return 1;
        else if(cmpval < 0)
            last = middle - 1;
        else
            first = middle + 1;
    }
    return 0;
}
ajay
  • 9,402
  • 8
  • 44
  • 71