C 数组中的二进制搜索

C 数组中的二进制搜索,c,arrays,binary-search,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)

有人能纠正并完成下面的代码吗?我不能那样做

首先,我要从一个值为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;

}
我为此奋斗了好几个小时

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

编辑:对于每个语句,循环中变量“低”和“高”的值应该是多少

#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;

}
#
#包括
#包括
#包括
int main(){
srand(时间(空));
int数组[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=数组[randomIndex];
int低=0;
int高=19;
int-middle=(低+高)/2;
printf(“要罚款的随机数为%d\n”,随机值);
while(中间值){
printf(“数字%d低于随机数字\n”,中间);
低=;
高=;
中等=(低+高)/2;
}
如果(中间==随机值){
printf(“数字%d是正确的随机数”,中间);
}
}
返回0;
}

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

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

完整代码:

#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;

}
#包括
#包括
#包括
int main(){
srand(时间(空));
int数组[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=数组[randomIndex];
int低=0;
int高=19;
printf(“要查找的随机数为%d\n”,随机值);
而(高>=低){
int-middle=(低+高)/2;
if(数组[中间]<随机值){
printf(“数字%d小于随机数”,数组[middle]);
低=中+1;
}
if(数组[中间]>随机值){
printf(“数字%d小于随机数”,数组[middle]);
高=中-1;
}
if(数组[中间]==随机值){
printf(“数字%d是正确的随机数”,数组[middle]);
打破
}
}
返回0;
}

你在20岁时就开始了
。但是,这是一个指数,所以你想要19

您的
while
循环条件错误<代码>中间值
可以大于搜索中的
随机值
。您确实希望比较

您需要两个变量:一个用于当前索引,一个用于当前值,而不仅仅是单个
中间值。“value”变量应该是从数组索引中获取的值

您必须根据循环中的测试更改中间索引值

当获得匹配项时,您必须使用
break
停止循环


以下是您的代码版本,其中已注释并修复了错误:

#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;
// NOTE/BUG: high is an _index_ so it must be one less
#if 0
    int high = 20;
#else
    int high = 19;
#endif
// NOTE/BUG: this must be done inside the loop
#if 0
    int middle = (low + high) / 2;
#else
    int middle;
    int middle_index;
#endif

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

// NOTE/BUG: wrong loop condition -- middle can be higher during search
// NOTE/BUG: in the general case we need a two variables: one for current
// index and one for current value
#if 0
    while (middle <= randomValue) {
#else
    while (low <= high) {
#endif
        middle_index = (low + high) / 2;
        middle = array[middle_index];

// NOTE/BUG: none of these change low/high
        if (middle < randomValue) {
            printf("The number %d is lower than the random number\n", middle);
#if 1
            low = middle_index + 1;
#endif
        }

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

        if (middle == randomValue) {
            printf("The number %d  is the correct random number\n", middle);
#if 1
            break;
#endif
        }
    }

    return 0;
}
#包括
#包括
#包括
int
main()
{
srand(时间(空));
int数组[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=数组[randomIndex];
int低=0;
//注意/错误:high是一个_索引,所以它必须少一个
#如果0
int高=20;
#否则
int高=19;
#恩迪夫
//注意/错误:这必须在循环内完成
#如果0
int-middle=(低+高)/2;
#否则
中间;
int-middle_指数;
#恩迪夫
printf(“要查找的随机数为%d\n”,随机值);
//注意/错误:循环条件错误--在搜索过程中,中间值可能更高
//注意/错误:在一般情况下,我们需要两个变量:一个用于当前
//索引和一个用于当前值
#如果0

虽然(中间你的问题是什么?你在寻找代码的反馈吗?它不起作用,你需要调试帮助吗?你需要在循环中重新计算
middle
,并根据你是向左还是向右更新
low
high
。你可能需要担心
high=20;
vs
high=19类似地,在循环中,计算
middle=(低+高)/2;
,当您做出方向选择时,调整
@qwerty1805
while
是一个循环,但条件是错误的,而且条件的值在循环体内部从未更改,因此循环是无限的或从未执行过。您需要在t中的某个位置添加一个
break
他循环,否则它是一个无限循环。
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;

}
#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;
// NOTE/BUG: high is an _index_ so it must be one less
#if 0
    int high = 20;
#else
    int high = 19;
#endif
// NOTE/BUG: this must be done inside the loop
#if 0
    int middle = (low + high) / 2;
#else
    int middle;
    int middle_index;
#endif

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

// NOTE/BUG: wrong loop condition -- middle can be higher during search
// NOTE/BUG: in the general case we need a two variables: one for current
// index and one for current value
#if 0
    while (middle <= randomValue) {
#else
    while (low <= high) {
#endif
        middle_index = (low + high) / 2;
        middle = array[middle_index];

// NOTE/BUG: none of these change low/high
        if (middle < randomValue) {
            printf("The number %d is lower than the random number\n", middle);
#if 1
            low = middle_index + 1;
#endif
        }

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

        if (middle == randomValue) {
            printf("The number %d  is the correct random number\n", middle);
#if 1
            break;
#endif
        }
    }

    return 0;
}
#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;
    int middle;
    int middle_index;

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

    while (low <= high) {
        middle_index = (low + high) / 2;
        middle = array[middle_index];

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

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

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

    return 0;
}