为什么我的二进制搜索返回" true"什么时候找不到元素?

时间:2016-09-01 19:25:28

标签: c cs50

find.c

/**
 * find.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Prompts user for as many as MAX values until EOF is reached, 
 * then proceeds to search that "haystack" of values for given needle.
 *
 * Usage: ./find needle
 *
 * where needle is the value to find in a haystack of values
 */

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>


#include "helpers.h"

// maximum amount of hay
const int MAX = 65536;

int main(int argc, string argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: ./find needle\n");
        return -1;
    }

    // remember needle
    int needle = atoi(argv[1]);

    // fill haystack
    int size;
    int haystack[MAX];
    for (size = 0; size < MAX; size++)
    {
        // wait for hay until EOF
        printf("\nhaystack[%i] = ", size);
        int straw = GetInt();
        if (straw == INT_MAX)
        {
            break;
        }

        // add hay to stack
        haystack[size] = straw;
    }
    printf("\n");

    // sort the haystack
    sort(haystack, size);

    // try to find needle in haystack
    if (search(needle, haystack, size))
    {
        printf("\nFound needle in haystack!\n\n");
        return 0;
    }
    else
    {
        printf("\nDidn't find needle in haystack.\n\n");
        return 1;
    }
}

helpers.c

 /**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */

#include <cs50.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    int temp = values[n/2];
    do {
            if (temp == value)
            {
                return true;
            }
            else if (temp < value)
            {
               temp = temp + temp / 2;
            }
            else if (temp > value)
            {
              temp = temp - temp / 2;
            }
        } while(temp > 1);
    return false;   
}



/**    for (int i = 0; i < n; i++)
*    {
*       if (values[i] == value)
*        {
*            return true;
*        }
*    }
*    return false;
*}
*/
/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    int swaps = -1;
    do {
        swaps = 0;
        for(int i = 0, temp; i < n; i++)
        {
            if(values[i] > values[i+1])
            {
                temp = values[i+1];
                values[i+1] = values[i];
                values[i] = temp;
                swaps = swaps + 1;
            }
        }
    }while(swaps != 0);
    return;
}

我很难过。搜索功能是helpers.c文件中的功能。当我检查程序时,它会返回0&#39;即使在数组中找不到数字也是如此。我很想知道为什么要这样做。

1 个答案:

答案 0 :(得分:1)

你应该将每个时间值[temp]与值而不是temp与值进行比较。你也不应该给出值[n / 2]而是n / 2,而你的实现并没有涵盖value是值[0],因为你有条件while(temp >1)所以temp总是>> 1,如果value是值[2](例子值[] = {1,2,3,4,5} },value = 5)。
所以你应该添加到最后:

if (values[1]==value || values[0]==value ) return true;

最后,如果值大于值的最大元素,则temp将不断递增,因此您将拥有无限循环,因此我将条件更改为:

while(temp >1 && temp<n);

根据评论中的建议,您可以通过保留低,中,上限变量而不是仅使用临时变量来更好地编写搜索功能。

   bool search(int value, int values[], int n)
{
    int temp = n/2;
    do {
            if (values[temp] == value)
            {
                return true;
            }
            else if (values[temp] < value)
            {
               temp = temp + temp/ 2;
            }
            else if (values[temp] > value)
            {
              temp = temp /2; //temp-temp/2 equals to temp/2
            }
        } while(temp >1 && temp<n);
    if (values[1]==value || values[0]==value ) return true;
    return false;   
}