C:为什么我的二分查找会陷入无限循环?

发布于 2024-11-04 07:27:18 字数 1597 浏览 0 评论 0原文

我有一个程序,它使用二分搜索来搜索文件(numbers.dat)并打印该值是否在数组中。目前,当我想搜索numbers.dat文件中的第一个值或不在numbers.dat文件中的值时,我会遇到无限循环,并且如果我想搜索文件中的任何其他值它打印索引和未找到消息。

这是我的代码:

 int main() {
     FILE *in_file; /* Input file */
     int middle;        /* Middle of our search range */
     int low, high; /* Upper/lower bound */
     int search;        /* number to search for */
     char line[80]; /* Input line */

     in_file = fopen(DATA_FILE, "r");
     if (in_file == NULL) {
          fprintf(stderr,"Error:Unable to open %s\n", DATA_FILE);
          exit (8);
     }

     /*
      * Read in data
      */

     max_count = 0;
     while (1) {
          if (fgets(line, sizeof(line),  in_file) == NULL)
          break;

          /* convert number */
          sscanf(line, "%d", &data[max_count]);
          ++max_count;
     }

    while (1) {
        printf("Enter number to search for or -1 to quit:" );
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &search);

        if (search == -1)
            break;

       low = 0;
        high = max_count;

       while (1) {
             middle = (low + high) / 2;

         if (data[middle] == search) {
         printf("Found at index %d\n", middle);

         }

         if (low == high) {
         printf("Not found\n");
         break;
         }

         if (data[middle] < search)
         low = (middle + 1);
         else
         high = (middle - 1);
     }
    }
     return (0);
}

numbers.dat 文件的前几行是: 4 6 14 16 17 号 如果我搜索 4 或说 2,我会得到一个无限循环,如果我搜索 6,我会得到:
在索引 1 处找到
未找到

I have a program that searches a file (numbers.dat) using a binary search and prints whether the value is in the array or not. Currently, when I want to search for the first value in the numbers.dat file or for a value that is not in the numbers.dat file, I get an infinite loop, and if I want to search for any other value in the file it prints he index and Not found messages.

Here is my code:

 int main() {
     FILE *in_file; /* Input file */
     int middle;        /* Middle of our search range */
     int low, high; /* Upper/lower bound */
     int search;        /* number to search for */
     char line[80]; /* Input line */

     in_file = fopen(DATA_FILE, "r");
     if (in_file == NULL) {
          fprintf(stderr,"Error:Unable to open %s\n", DATA_FILE);
          exit (8);
     }

     /*
      * Read in data
      */

     max_count = 0;
     while (1) {
          if (fgets(line, sizeof(line),  in_file) == NULL)
          break;

          /* convert number */
          sscanf(line, "%d", &data[max_count]);
          ++max_count;
     }

    while (1) {
        printf("Enter number to search for or -1 to quit:" );
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%d", &search);

        if (search == -1)
            break;

       low = 0;
        high = max_count;

       while (1) {
             middle = (low + high) / 2;

         if (data[middle] == search) {
         printf("Found at index %d\n", middle);

         }

         if (low == high) {
         printf("Not found\n");
         break;
         }

         if (data[middle] < search)
         low = (middle + 1);
         else
         high = (middle - 1);
     }
    }
     return (0);
}

The numbers.dat file's first few lines are:
4
6
14
16
17
And if I search for 4 or say 2, I get an infinite loop and if I search for 6 I get:

Found at index 1
Not found

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-11-11 07:27:18
  1. 如果低>,您应该打破未找到的状态高的。
  2. 如果找到了,就不会中断,因此您可以进行另一次迭代。
  3. 你总是检查中间。如果高或低有所需的结果怎么办?
  1. You should break with not found if low > high.
  2. You don't break if you found, so you go to another iteration.
  3. You always check for middle. what if high or low have the needed result?
寒江雪… 2024-11-11 07:27:18

以下代码:

   while (1) {
         middle = (low + high) / 2;

     if (data[middle] == search) {
     printf("Found at index %d\n", middle);

     }

不退出循环。弄清楚你需要做什么才能摆脱这种情况。

The following code:

   while (1) {
         middle = (low + high) / 2;

     if (data[middle] == search) {
     printf("Found at index %d\n", middle);

     }

Does not exit the loop. Figure out what you need to do to get out of that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文