简体   繁体   English

这是一个二进制搜索代码,我无法找出问题所在

[英]it is a binary search code and i m not able to figure out the problem

Here is my code.The code is running but not getting the right results.这是我的代码。代码正在运行,但没有得到正确的结果。 For ex- searching 3 in the program is getting output "not found" even though present.例如,在程序中搜索 3 会得到输出“未找到”,即使存在。

 #include<stdlib.h>
 #include<iostream>
using namespace std;
int Bsearch(int arr[],int item,int n)
{
 int low,mid,up;
 low=0;up=n-1;
 while(low<=up && item!=arr[mid])
 {
     mid=(low+up)/2;
     if(item==arr[mid])
        return mid;
     else if(item<arr[mid])
        low=mid+1;
     else
        up=mid-1;
 }
 return -1;


}
int main()
{
    int index,item;;
    int arr[]={1,2,3,4,5,6,7,8,9};

  cout<<"enter search item\n";
  cin>>item;
    index=Bsearch(arr,item,9);
    if(index!=-1)
    cout<<"element found at position"<<(index+1);
    else
    cout<<"element not found";

    return 0;
}

Here is the problem:这是问题所在:

if(item==arr[mid])
        return mid;
     else if(item<arr[mid])
        low=mid+1;
     else
        up=mid-1;

just change the conditon of choosing low and up index according the value of mid index arr[mid] like:只需根据中索引arr[mid]的值更改选择low索引和up索引的条件,例如:

 if(item==arr[mid])
    return mid;
 else if(item<arr[mid])
    up=mid-1;
 else
    low=mid+1;

OR change this condition else if(item<arr[mid]) into else if(item > arr[mid]) and your code will work fine.或将此条件else if(item<arr[mid])更改为else if(item > arr[mid])并且您的代码将正常工作。

if(item==arr[mid])
        return mid;
     else if(item > arr[mid])
        low=mid+1;
     else
        up=mid-1;

NB: while(low<=up && item!=arr[mid]) in this line, you're using mid in arr[mid] before it has been assigned a value, resulting in Undefined Behavior.注意: while(low<=up && item!=arr[mid])在这一行中,您在arr[mid]使用mid ,然后才为其分配值,从而导致未定义行为。 (Also, the && item!=arr[mid] part of the while test is unnecessary.) Credit: 1201ProgramAlarm (此外,while 测试的&& item!=arr[mid]部分是不必要的。)信用:1201ProgramAlarm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码有问题,因为我不知道为什么会收到错误。 这是代码: - I'm having issue with my code, in that I can't figure out why I'm receiving an error. Here is the code: 分段错误(核心转储)无法调试具有重复问题的二进制搜索代码? - Segmentation fault (core dumped) not able to debug the code for binary search with duplicates problem? 我执行了以下代码并且我的系统卡住了,但我无法找出背后的原因? - I executed the following code and my system stuck, but I am not able to figure out reason behind it? 我不明白为什么我的代码出现段错误 - I can't figure out why I'm getting a seg fault in my code (“向量下标超出范围”)用于二分搜索问题 - (“vector subscript out of range”) for Binary search problem 我的代码不断收到 SIGTSTP 错误,但我无法弄清楚我的代码在哪里越界 - I keep getting SIGTSTP error for my code but I am not able to figure out where is my code going out of bounds 我的代码没有编译,任何人都可以帮我弄清楚我做错了什么? - My Code isn't compiling, can anyone help me figure out what I'm doing wrong? 我正在阅读开源代码(C++),但不知道为什么他们这样放置枚举 - I'm reading open source code (C++) and can't figure out why they put enum this way 我无法弄清楚我的代码没有显示预期结果的问题 - I can't figure out the problem with my code not showing expected results 二进制搜索树代码用于插入操作的问题 - Problem with Binary Search tree code for insert operation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM