c - 数组中的二分查找

标签 c arrays binary-search

有人可以更正并完成下面的代码吗?我无法做到这一点...

我首先想要一个从值 1 到 20 的数组生成的随机数。

程序必须通过在不同阶段猜测数组中间的数字来找到随机数,并在每次循环后消除剩余数字的一半。

假设随机数是 13

由于数组介于 1 和 20 之间,因此第一个猜测数字是 10,因为这是数组中间的数字。

由于猜测数字 10 小于随机数 13,因此下一次测试为 15(对应于 ( 20 +10 )/2)。

由于猜测数字 15 大于随机数 13,因此下一次测试为 12(对应于 ( 15 +10 )/2)。

由于猜测数 12 小于随机数 13,因此下一次测试为 13(对应于 ( 12+15 )/2)。

猜测数字现在与随机数字匹配

这是我的代码

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

int main() {

    srand(time(NULL));
    int array [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
    int randomIndex = rand() % 20;
    int randomValue = array[randomIndex];
    int low = 0;
    int high = 20;
    int middle = (low + high) / 2;


    printf("The random number to find is %d\n", randomValue);

    while (middle <= randomValue) {

        if (middle < randomValue) {
            printf("The number %d is lower than the random number\n", middle);

        }

        if (middle > randomValue) {
            printf("The number %d is lower than the random number\n", middle);
        }

        if (middle == randomValue) {
            printf("The number %d  is the correct random number", middle);
        }

    }
    return 0;

}

并且有预期的输出

预期输出(13作为随机数):

The number 10 is lower than the random number

The number 15 is higher than the random number

The number 12 is lower than the random number

The number 13 is the correct random number

为了做到这一点,我挣扎了几个小时。

任何帮助将不胜感激。提前谢谢您。

已编辑:每个语句的循环中变量“low”和“high”的值应该是多少?

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

int main() {

    srand(time(NULL));
    int array [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
    int randomIndex = rand() % 19;
    int randomValue = array[randomIndex];
    int low = 0;
    int high = 19;
    int middle = (low + high) / 2;


    printf("The random number to fine is %d\n", randomValue);

    while (middle <= randomValue) {

        if (middle < randomValue) {
            printf("The number %d is lower than the random number\n", middle);
            low  = ;
            high = ;
            middle = (low + high) / 2;
        }

        if (middle > randomValue) {
            printf("The number %d is lower than the random number\n", middle);
            low  = ;
            high = ;
            middle = (low + high) / 2;

        }

        if (middle == randomValue) {
            printf("The number %d  is the correct random number", middle);
        }

    }
    return 0;

}

最佳答案

您应该将数组 (array[middle]) 的值与 randomValue 进行比较,因为如果数组不是从 120 就像你所做的那样(例如,int array [20] = {0, 3, 4, 10, 15, ...}),你的程序永远不会正确.

while循环的代码(代码注释中说明):

while (high >= low) {
        int middle = (low + high) / 2; // update middle in each iteration of while loop

        if (array[middle] < randomValue) {
            printf("The number %d is lower than the random number\n",array[middle]);
            low = middle+1; // If randomValue greater than value at middle position, we can ignore left half 

        }

        if (array[middle] > randomValue) {
            printf("The number %d is lower than the random number\n", array[middle]);
            high = middle - 1; // If randomValue smaller than value at middle position, we can ignore right half
        }

        if (array[middle] == randomValue) {
            printf("The number %d  is the correct random number", array[middle]);
            break; // exit while loop if you find out the number.
        }

    }

randomValue = 13 时的输出:

The random number to find is 13                                                                                                                             
The number 10 is lower than the random number                                                                                                               
The number 15 is lower than the random number                                                                                                               
The number 12 is lower than the random number                                                                                                               
The number 13  is the correct random number

完整代码:

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

int main() {

    srand(time(NULL));
    int array [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
    int randomIndex = rand() % 20;
    int randomValue = array[randomIndex];
    int low = 0;
    int high = 19;


    printf("The random number to find is %d\n", randomValue);

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

        if (array[middle] < randomValue) {
            printf("The number %d is lower than the random number\n",array[middle]);
            low = middle+1;

        }

        if (array[middle] > randomValue) {
            printf("The number %d is lower than the random number\n", array[middle]);
            high = middle - 1;
        }

        if (array[middle] == randomValue) {
            printf("The number %d  is the correct random number", array[middle]);
            break;
        }

    }
    return 0;

}

关于c - 数组中的二分查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61937309/

相关文章:

c - Lua 用户数据 : Unable to have simultaneous array access and methods

c - 使用malloc分配不同行长的多维数组

objective-c - 如何在xcode中找到string.h的包含路径

java - java中随机数组与随机数组相加,使其等于另一个数组

unity-game-engine - 来自 byte[] 数组的位图

javascript - Else 语句运行两次 jQuery

java - 使用二分搜索返回的缺失元素位置时的代码更清晰

c - 尝试计算函数的时间和存储复杂度 (C)

python - 二分查找函数,取决于偶数和奇数输入

algorithm - std::binary_search 未按预期工作