问题二分查找算法 [英] problem with binarysearch algorithm

查看:129
本文介绍了问题二分查找算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code以下属于二进制搜索算法,用户在TextBox1中输入数字,然后输入他想与二分查找到Fing头在textbox2.i数量已经出了问题,那就是当我输入,例如15, 21在TextBox1中,然后在TextBox2中15,把brakpoint就行了我下面的评论,我明白,它不认为这一数字在TextBox2中的searchnums(评论),为更多的解释我在code.thanks提前发表评论

 公共无效的button1_Click(对象发件人,EventArgs的)
    {


        INT searchnums = Convert.ToInt32(textBox2.Text); //问题就在这里,在TextBox2中犯规的价值存在于searchnums和它的价值0。

        INT结果=二分查找(searchnums);
        的MessageBox.show(result.ToString());
    }
    公众诠释二分查找(INT searchnum)
    {
        字符串[]来源= textBox1.Text.Split('');
        INT [] = NUM​​S新INT [source.Length]
        的for(int i = 0; I< source.Length;我++)
        {
            NUMS [I] = Convert.ToInt32(来源[I]);
        }

        诠释第= 0;

        INT最后= nums.Length-1;


        而(1< = nums.Length)
        {
        INT中期=(int)的Math.Floor(第一+最后/ 2.0);
             如果(第一>上)
            {
                打破;
            }
            如果(searchnum< NUMS [MID])
            {
                最后=中旬 -  1;
            }
            如果(searchnum> NUMS [MID])
            {
                第一=中等+ 1;
            }
            其他
            {
                回报NUMS [MID];

            }
        }
        返回-1;


    }
 

解决方案

首先,当你知道你可以放置一个断点上线,你肯定知道你可以通过在程序步骤,看看什么值都在每个变量或属性。因此,继续和调试。除此之外,你很可能缺少在程序中的数据输入验证。

二,你的二进制搜索实现缺少中期之后重新计算第一最后更新,并在案件的数量正在寻找没有在数组中找到了一个停止条件。你必须打破循环的情况下第一==最后,不管 NUMS [MID] 匹配与否 - 没有人说,从 TextBox2中数量的必须的是数组中

the code below belongs to binary search algorithm,user enter numbers in textbox1 and enter the number that he want to fing with binarysearch in textbox2.i have a problem with it,that is when i enter for example 15,21 in textbox1 and enter 15 in textbox2 and put brakpoint on the line i commented below,and i understood that it doesnt put the number in textbox2 in searchnums(commented),for more explanation i comment in code.thanks in advance

 public void button1_Click(object sender, EventArgs e)
    {


        int searchnums = Convert.ToInt32(textBox2.Text);//the problem is here,the value in textbox2 doesnt exist in searchnums and it has value 0. 

        int result = binarysearch(searchnums);
        MessageBox.Show(result.ToString());
    }
    public int binarysearch(int searchnum)
    {
        string[] source = textBox1.Text.Split(',');
        int[] nums = new int[source.Length];
        for (int i = 0; i < source.Length; i++)
        {
            nums[i] = Convert.ToInt32(source[i]);
        }

        int first =0;

        int last = nums.Length-1;


        while (1<= nums.Length)
        {
        int mid = (int)Math.Floor(first+last / 2.0);
             if (first > last)
            {
                break;
            }
            if (searchnum < nums[mid])
            {
                last = mid - 1;
            }
            if (searchnum > nums[mid])
            {
                first = mid + 1;
            }
            else
            {
                return nums[mid];

            }
        }
        return -1; 


    }

解决方案

First, when you know you can place a breakpoint on a line, you sure know you can step through the whole program and see what values are in each variable or property. So, go ahead, and debug. Among other things you are probably missing data input validation in your program.

Second, your binary search implementation is missing mid recalculation after first or last is updated and a stop condition in case the number being looked for is not found in the array. You have to break the loop in case first == last, regardless if nums[mid] matches or not — nobody said the number from textbox2 must be in the array.

这篇关于问题二分查找算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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