我的二分搜索只会找到位于数组中间的元素

发布于 2025-01-10 06:31:22 字数 1291 浏览 0 评论 0原文

所以我试图进行二分搜索来按姓氏查找,但我的代码只会查找我搜索的元素是否位于数组中间的元素,否则它将冻结并崩溃,有人知道我在做什么吗?做错了吗?谢谢。

      private int binarySearch(string surnameSearched)
    {
        int start = 0;
        int end = direcTable.Length - 1;
        bool found = false;
        bool allListSearched = false;
        int mid = (start + end) / 2;
        while ((!found) && (!allListSearched))
        {
            lvDirectory.Items.Clear();
            if (String.Compare(direcTable[mid].surname, surnameSearched, true) == 0)
            {
                found = true;
            }
            else if (start > end)
            {
                allListSearched = true;
            }
            else if (String.Compare(direcTable[mid].surname, surnameSearched) < 0)
            {
                end = mid - 1;
            }
            else
            {
                start = mid + 1;
            }
        }
        if (found)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = direcTable[mid].extCode;
            lvi.SubItems.Add(direcTable[mid].forename);
            lvi.SubItems.Add(direcTable[mid].surname);
            lvDirectory.Items.Add(lvi);
            return (mid);

        }
        else
        {
            return -1;
        }

So I'm trying to make a binary search to find out by the surname but my code will only find if the element I search is the one located in the middle of the arrays, otherwise it will freeze and crash, does anyone knows what I'm doing wrong? Thank you.

      private int binarySearch(string surnameSearched)
    {
        int start = 0;
        int end = direcTable.Length - 1;
        bool found = false;
        bool allListSearched = false;
        int mid = (start + end) / 2;
        while ((!found) && (!allListSearched))
        {
            lvDirectory.Items.Clear();
            if (String.Compare(direcTable[mid].surname, surnameSearched, true) == 0)
            {
                found = true;
            }
            else if (start > end)
            {
                allListSearched = true;
            }
            else if (String.Compare(direcTable[mid].surname, surnameSearched) < 0)
            {
                end = mid - 1;
            }
            else
            {
                start = mid + 1;
            }
        }
        if (found)
        {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = direcTable[mid].extCode;
            lvi.SubItems.Add(direcTable[mid].forename);
            lvi.SubItems.Add(direcTable[mid].surname);
            lvDirectory.Items.Add(lvi);
            return (mid);

        }
        else
        {
            return -1;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2025-01-17 06:31:22

       while (start < end)
       {
           int mid = start + (end - start) / 2;
           lvDirectory.Items.Clear();
           if (String.Compare(direcTable[mid].surname, surnameSearched, true) == 0)
           {
               found = true;
           }
         
           if (String.Compare(direcTable[mid].surname, surnameSearched) < 0) // I Dont know on what bases your comparing in this If statment
           {
               end = mid ;
           }
           else
           {
               start = mid + 1;
           }
       }

       while (start < end)
       {
           int mid = start + (end - start) / 2;
           lvDirectory.Items.Clear();
           if (String.Compare(direcTable[mid].surname, surnameSearched, true) == 0)
           {
               found = true;
           }
         
           if (String.Compare(direcTable[mid].surname, surnameSearched) < 0) // I Dont know on what bases your comparing in this If statment
           {
               end = mid ;
           }
           else
           {
               start = mid + 1;
           }
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文