通过字符串数组进行二进制搜索 - C.

时间:2012-10-12 23:04:24

标签: c arrays binary-search

所以我试图使用二进制搜索在数组中的单词字典中查找某个单词。目前我的功能看起来像这样,

void binsearch(char letters[], char changeletters[], char **dictionary[], int numwords){

printf("start binsearch\n");
int max = numwords;
int min = 0;
int mid;
int spot;

while (min <= max){

printf("start while in search\n");

    mid = (min + max) / 2;
    spot = strcmp(**dictionary[mid],changeletters);
    printf("%d", spot);

    if (spot == 0){
        printf("word found\n");
        printf("A permutation of %s that is a valid word is %s", letters, changeletters);
    }
    else if (spot < 0){
        printf("word not found and word is lower\n");
        min=spot+1;
    }
    else{
        printf("word no found and word is higher\n");
        max=spot-1;
    }
 }

     if( max > min)
         printf("Sorry, no permutations of %s form a word", letters);
}

随机printfs用于检查程序的获取程度。目前它在字符串比较失败,我不知道为什么。任何想法?

0 个答案:

没有答案