用C语言进行二进制搜索

时间:2014-01-03 04:25:37

标签: c

下一个代码有什么问题? 它是一个代码,用于在我们使用rand()函数随机填充的数组元素之间进行二进制搜索。我们在这里使用函数bin_sear来为我们排序并为我们返回一个布尔值true如果我们在我们的表中找到了我们寻找的元素,并且如果我们没有找到的元素在我们的表中找到了数字。代码执行工作。那么这里的错误在哪里?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef enum { false, true } bool;

bool bin_sear(int k[] ,int s , int target );

int main()
{
int a[10];
int i,j;
char k;
bool bin;

while(1){
srand(time(NULL));
// we fill our table
  for(i=0;i<10;i++){
    a[i]=rand()%101+1;

  }
printf("Please enter the element you are seeking for: ");
scanf("%d",&j);

  if(bin_sear(a[10],10,j)==true){
  printf("The element you seek exists in our array");
  }
  else {
    printf("The element you are seeking doesent exist in our array");
  }
        if (k=='n'){
    break;
  }
}

return 0;
}


bool bin_sear(int k[],int s, int target){

int i,j,pos,aux,low,high,test;

for (i=0;i<s;i++){
    pos=i;
    for (j=i;j<s;j++){
        if (k[j]<pos){
            pos=j;
        }
     aux=k[pos];
     k[pos]=k[i];
     k[i]=aux;
    }
}
low=0;
high=s;


while (low<high-1){

     test=(low+high)/2;

if (target<k[test]){

    high=test;


}
else low=test;
}

if (target==k[test]){

    return true;
}
else return false;
}

3 个答案:

答案 0 :(得分:4)

您的代码中存在许多错误。

您需要使用指向数组的指针调用bin_sear,而不是元素a[10](这将为您提供即时分段错误):

if(bin_sear(a[10],10,j)==true){

应该是

  if(bin_sear(a,10,j)==true){

接下来 - 看看你的冒泡排序例程。您的索引错误,并且大括号位于错误的位置,并且您正在使用if语句将值与索引进行比较。将其修改为此,您将获得一个已排序的数组:

for (i=0;i<s-1;i++){
    pos=i;
    for (j=i+1;j<s;j++){
        if (k[j]<k[i]){
            pos=j;
            aux=k[pos];
            k[pos]=k[i];
            k[i]=aux;
        }
    }
}

接下来,您似乎没有在任何地方设置k的值,因此您有一个无限循环。修复所有这些,事情会好一点。如果这还不够,建议你在代码中添加很多printf语句(最初使用较小范围的随机数来提高命中率)。

工作代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef enum { false, true } bool;

bool bin_sear(int k[] ,int s , int target );

int main()
{
int a[10];
int i,j;
char k;
bool bin;

while(1){
srand(time(NULL));
// we fill our table
  for(i=0;i<10;i++){
    a[i]=rand()%10+1;
  }

  printf("Please enter the element you are seeking for: \n");
  // scanf("%d", &j); // not scanning input since this is codepad
  j = 5;
  printf("you entered: %d\n", j);
  fflush(stdout);

  if(bin_sear(a,10,j)==true){
    printf("The element [%d] exists in our array\n", j);
  }
  else {
    printf("The element [%d] doesn't exist in our array\n", j);
    printf("The array contains:\n");
    for(i = 0; i < 9; i++) printf("%d, ", a[i]);
    printf("%d\n", a[9]);
  }
   printf("Would you like to test for another element?\n");
  // k = getchar();
  k = 'N'; // simulating user pressing uppercase N
  if(tolower(k) == 'n') {
     printf("goodbye!\n");
     break; // do this only once
  }
}

return 0;
}


bool bin_sear(int k[],int s, int target){

int i,j,pos,aux,low,high,test;

printf("starting to sort\n");
fflush(stdout);
for (i=0;i<s-1;i++){
    pos=i;
    for (j=i+1;j<s;j++){
        if (k[j]<k[i]){
            printf("swapping %d and %d\n", i, j); fflush(stdout);
            pos=j;
            aux=k[pos];
            k[pos]=k[i];
            k[i]=aux;
        }
    }
}
printf("sort finished\n");
printf("elements now:\n");
for(i = 0; i < s; i++) printf("k[%d] = %d\n", i, k[i]);
fflush(stdout);
low=0;
high=s;


while (low<high-1){

     test=(low+high)/2;

if (target<k[test]){

    high=test;


}
else low=test;
}

if (target==k[test]){

    return true;
}
else return false;
}

答案 1 :(得分:1)

if(bin_sear(a[10],10,j)==true){ =&gt; if(bin_sea(a, 10, j) == true)快速浏览一下。你需要将数组传递给函数,a[10]会尝试索引数组的第11个元素,也是错误的类型(是int而不是数组),并且可能会导致访问冲突异常,因为数组只有十个元素长。四个字符有很多错误,呃?

答案 2 :(得分:0)

编写一个对数组进行排序然后进行二进制搜索的函数是没有意义的。您应该将这两种算法分开,否则线性搜索会更有效。