【问题标题】:Code for Binary search in C not working properlyC中的二进制搜索代码无法正常工作
【发布时间】:2020-05-01 11:18:05
【问题描述】:

我无法修复逻辑错误,因为我不知道这段代码有什么问题。每个输入,它都显示“未找到元素”。如果有人可以帮助我,我将不胜感激。同样在这段代码中,我假设我们将数组的大小作为奇数,如果我们决定将偶数作为大小怎么办?

#include<stdio.h>
int main(){
  int size;
  printf("Enter the number of elemets(odd number) : ");
  scanf("%d",&size);
  int arr[size];
  printf("Enter the elements in ascending order : ");
  for(int i=0;i<size;i++){
    scanf("%d",&arr[i]);
  }
  int element;
  int flag=0;
  printf("Enter element to be found : ");
  scanf("%d",&element);
  int low=0;
  int high=size-1;
  while(low<high){
    int mid=(low+high)/2;

    if(element<arr[mid]){
      high=mid-1;
    }
    else if(element>arr[mid]){
      low=mid+1;
    }
    else if(element==arr[mid]){
      printf("Element %d found at pos %d ",element,mid);
      flag=1;
      break;
    }
  }
  if(flag==0){
    printf("Element not found");
  }

  return 0;
}

【问题讨论】:

  • 请分享一个您的代码不起作用的示例输入。
  • 为了调试这个你应该有一个硬编码的数组和一个硬编码的元素来搜索,这样你就不必在每次运行时输入数组和值来搜索
  • OT:在使用可变宽度字符的系统上,2 个空格的缩进将被“丢失” 建议每个缩进级别使用 4 个空格

标签: c arrays sorting if-statement binary-search


【解决方案1】:

问题在于您的while 测试。你有:

while(low<high) {
    ...
}

low == high 所需的值在该位置时,这将失败。通过将测试更改为:

while(low <= high) {
    ...
}

这就是修复它所需的全部内容。您不需要添加任何特殊情况来“修复它”。只要确保你的数组是升序的,它应该可以工作。

【讨论】:

    【解决方案2】:

    编辑:参考@TomKarzes 的更好答案

    我的旧答案是:

    你错过了高==低的边界情况

    #include<stdio.h>
    int main(){
      int size;
      printf("Enter the number of elements(odd number) : ");
      scanf("%d",&size);
      int arr[size];
      printf("Enter the elements in ascending order : ");
      for(int i=0;i<size;i++){
        scanf("%d",&arr[i]);
      }
      int element;
      int flag=0;
      printf("Enter element to be found : ");
      scanf("%d",&element);
      int low=0;
      int high=size-1;
      while(low<high){
        int mid=(low+high)/2;
    
        if(element<arr[mid]){
          high=mid-1;
        }
        else if(element>arr[mid]){
          low=mid+1;
        }
        else if(element==arr[mid]){
          printf("Element %d found at pos %d ",element,mid);
          flag=1;
          break;
        }
      }
      if(low==high && arr[low]==element) //Added 1 extra condition check that you missed
      {
        printf("Element %d found at pos %d ",element,low);
        flag=1;
      }
      if(flag==0){
        printf("Element not found");
      }
    
      return 0;
    }
    

    【讨论】:

      【解决方案3】:

      对于数组元素的数量,您使用size_t 类型。 int 类型的对象可以很小以容纳数组中的元素数量。

      循环的这个条件

      int high=size-1;
      while(low<high){
      //...
      

      不正确。例如,假设数组只有一个元素。在这种情况下,high 将等于 0,因此由于其初始化而等于 left

      int high=size-1;
      

      所以循环不会迭代,你会发现输入的数字在数组中没有找到,尽管数组的第一个和单个元素实际上等于数字。

      你需要改变条件

      while ( !( high < low ) )
      //...
      

      else 语句中的 if 语句

      else if(element==arr[mid]){
      

      是多余的。你可以写

      else // if(element==arr[mid]){
      

      如果将执行二分查找的代码放在单独的函数中会更好。

      这是一个演示程序,展示了如何编写这样的函数。

      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      
      int binary_search( const int a[], size_t n, int value )
      {
          size_t left = 0, right = n;  
          int found = 0;
      
          while ( !found && left != right )
          {
              size_t middle = left + ( right - left ) / 2;
      
              if (  value < a[middle] )
              {
                  right = middle;
              }
              else if ( a[middle] < value )
              {
                  left = middle + 1;
              }
              else
              {
                  found = 1;
              }
          }
      
          return found;
      }
      
      int cmp( const void *a, const void *b )
      {
          int left  = *( const int * )a;
          int right = *( const int * )b;
      
          return ( right < left ) - ( left < right );
      }
      
      int main(void) 
      {
          const size_t N = 15;
      
          srand( ( unsigned int )time( NULL ) );
      
          for ( size_t i = 0; i < N; i++ )
          {
              size_t n = rand() % N + 1;
      
              int a[n];
      
              for ( size_t j = 0; j < n; j++ ) a[j] = rand() % N;
      
              qsort( a, n, sizeof( int ), cmp );
      
              for ( size_t j = 0; j < n; j++ )
              {
                  printf( "%d ", a[j] );
              }
              putchar( '\n' );
      
              int value = rand() % N;
      
              printf( "The value %d is %sfound in the array\n",
                      value, binary_search( a, n, value ) == 1 ? "" : "not " );
          }
      
          return 0;
      }
      

      它的输出可能如下所示

      0 2 2 3 4 5 7 7 8 9 10 12 13 13 
      The value 5 is found in the array
      4 8 12 
      The value 10 is not found in the array
      1 2 6 8 8 8 9 9 9 12 12 13 
      The value 10 is not found in the array
      2 3 5 5 7 7 7 9 10 14 
      The value 11 is not found in the array
      0 1 1 5 6 10 11 13 13 13 
      The value 7 is not found in the array
      0 3 3 3 4 8 8 10 11 12 14 14 14 14 
      The value 3 is found in the array
      0 5 5 10 11 11 12 13 13 14 14 
      The value 12 is found in the array
      3 4 5 7 10 13 14 14 14 
      The value 14 is found in the array
      0 3 3 7 
      The value 2 is not found in the array
      1 6 9 
      The value 10 is not found in the array
      2 2 3 3 4 4 4 5 5 6 8 8 9 13 13 
      The value 11 is not found in the array
      11 11 13 
      The value 11 is found in the array
      0 0 0 1 2 5 5 5 7 7 8 9 12 12 14 
      The value 6 is not found in the array
      8 8 13 
      The value 1 is not found in the array
      2 2 4 4 5 9 9 10 12 12 13 13 14 14 
      The value 14 is found in the array
      

      【讨论】:

        猜你喜欢
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        • 2021-11-30
        • 1970-01-01
        • 2016-09-10
        • 2021-10-25
        • 2020-10-25
        • 1970-01-01
        相关资源
        最近更新 更多