二进制和线性搜索 [英] Binary and Linear searchs

查看:104
本文介绍了二进制和线性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在整理代码,但是做错了什么.我需要打开一个数组文件,要求用户输入要搜索的数字,然后完成线性搜索,重新加载文件并排序以进行二进制搜索.
我的问题是,当我输入搜索中未包含的数字时,它说找到了该数字(这是错误的).关于我可能做错了什么的任何建议.下面列出了遇到问题的代码Im.这是主要代码的一部分

I am putting together code but I am doing something wrong. I need to open a file of arrays, ask the user to input what number they want to search for, then complete a linear search, reload file, and sort to do a binary search.
My problem is when I enter numbers not included in the search its saying that the number is found(which is wrong). Any suggestions on what I might be doing wrong. The code Im having trouble with is listed below. This is part of the main code

int a = -1;
bs.Binary(unsorted, searchvalue);
if (a == -1)
    Console.Out.WriteLine("Element " + searchvalue + " was not found!");
else
    Console.Out.WriteLine("Element " + searchvalue + " was found.");



这是实现文件



This is the implementation file

    class LinearSearch<T>
    {
        public bool Linear(int[] A, int sv)
        {
            int count = 0;
            for (int i = 0; i < A.Length - 1; i++)
            {
                count++;
                if (sv == A[i])
                    return true;
            }
            return false;
        }   //Linear()
    }

    class BinarySearch<T>
    {
        public int Binary(int[] A, int sv)
        {
            int size = 100;
            int left = 0;
            int right = size - 1;
            while (left <= right)
            {
                int midpoint = (left + right) / 2;
                // check to see if value is equal to item in array
                if (sv == A[midpoint])
                {
                    return sv;
                }
                else if (A[midpoint] > sv)
                {
                    right = midpoint - 1;
                }
                else
                    left = midpoint + 1;
            }
            // item was not found
            return -1;
        }// Binary
    }
}



[删除的重复代码片段-亨利]



[Removed duplicate code snippet - Henry]

推荐答案

首先,在线性搜索中不需要"count"变量-您无需执行任何操作. br/>
第二:在二进制搜索中,应使用数组A的长度,而不是幻数"100".如果有50个元素,会发生什么?如果有200个怎么办?

除此之外,您的代码看起来应该可以工作(忽略您不使用的< T>部分).您确定排序工作正常吗?
Firstly, you don''t need the "count" variable in you linear search - you do nothing with it.

Secondly: you should use the Length of the array A, rather than the magic number "100" in your binary search. What happens if there are 50 elements? What if there are 200?

Other than that, you code looks like it should work (ignoring the <T> part you aren''t using). Are you sure your sort is working properly?


就像OriginalGriff所说的那样,您的搜索代码对我来说很好.

您得到的结果没有任何意义,因为无论您搜索什么数字都应该显示为未找到.

在您的调用代码中,您为a分配了值-1,但此后不更改.因此,只需进行少量更改即可解决此问题:
As OriginalGriff has said your search code looks fine to me.

The results you are getting do not make sense though as whatever number you search for should come up as NOT found.

In your calling code you assign a the value -1, but thereafter it is not changed. So a small change should cure this:
int a = bs.Binary(unsorted, searchvalue);  //<======= CHANGED ============
if (a == -1)
    Console.Out.WriteLine("Element " + searchvalue + " was not found!");
else
    Console.Out.WriteLine("Element " + searchvalue + " was found.");


做到了!
你摇滚亨利!
That did it!
You rock Henry!!!


这篇关于二进制和线性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆