使用二进制搜索搜索排序列表(C#)

时间:2013-03-18 07:56:39

标签: list search loops binary-search sorted

要求是使用二进制搜索方法搜索字母顺序排序/排序列表(字符串类型)指定字符串(在下面的示例中为“yz”)并返回结果(每个字符串以在类似ListBox的合适控件上指定字符串以及列表中该字符串的索引。

问题是BinarySearch方法必须在循环中运行多次,以便它返回列表中的所有匹配字符串而不只是一个。这是我的主要问题。我无法弄清楚如何正确地编写这个循环。如果您查看while循环内部,您将看到我正在尝试删除从列表中找到的结果,因此找不到它并再次返回。这会导致列表中项目索引的问题发生变化。所以它返回错误的结果索引。例如,如果你运行下面的代码,它应该返回索引:9,10,11和12.但是,我得到9,10,9,10,因为删除项目后,列表会变短。我可以用另一个随机字符串替换结果字符串而不是完全删除它,但这意味着列表不再按字母顺序排序。要使BinarySearch方法起作用,列表必须按字母顺序排序。

BinarySearch方法可能没问题,不需要任何更改。问题在于循环。我应该提到这是在我大学的作业,所以我不能使用任何快捷代码/内置函数。我已经试了好几个小时但却无法想出这个。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    string searchString = "yz";

    List<string> list1 = new List<string>();

    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
        if (list1.Count != 0)
        {
            list1.Clear();
        }

        list1.Add("ant");   //0
        list1.Add("bb");    //1
        list1.Add("bc");    //2 
        list1.Add("bD");    //3
        list1.Add("Be");    //4
        list1.Add("j");     //5
        list1.Add("RRR");   //6
        list1.Add("sT");    //7
        list1.Add("Uv");    //8      
        list1.Add("yz");    //9
        list1.Add("yza");   //10
        list1.Add("yzb");   //11
        list1.Add("yzc");   //12

        int index = BinarySearch(list1, 0, list1.Count, searchString);

        while (index != list1.Count)
        {
            listBox1.Items.Add("Index: " + index + " File: " + list1[index]);

            list1.RemoveAt(index); //Remove from list so we don't find it again
                                   // but this changes the index of the list

            index = BinarySearch(list1, 0, list1.Count, searchString);
        }
    }

    //BinarySearch method to search an alphabetically sorted list for a specified string
    public int BinarySearch(List<string> strList, int first, int last, string target)
    {
        int mid;    // index of the midpoint
        string midStr;  // object that is assigned listStr[mid]
        int origLast = last;
        // save original value of last
        while (first < last)
        // test for nonempty sublist
        {
            mid = (first + last) / 2;
            midStr = strList[mid];

            int indy = midStr.IndexOf(target, StringComparison.OrdinalIgnoreCase);

            if (indy == 0)
                return mid;// have a match, we're only matching the beginning of the string
            else
            {
                int order = string.Compare(target, midStr, StringComparison.OrdinalIgnoreCase);

                if (order < 0)
                    last = mid; // search lower sublist. Reset last                    
                else
                    first = mid + 1; // search upper sublist. Reset first
            }
        }
        return origLast; // target not found
    }
}

1 个答案:

答案 0 :(得分:1)

为什么不是,一旦你得到其中一个元素的索引,就从索引循环直到你到达最后一个对象或一个不从字符串开始的对象,从index-1到你得到的循环到0或你到达无效的对象。

编辑:要修改你的代码,我会做的是:

int index = BinarySearch(list1,0,list1.Count,searchString);

而不是执行删除对象的while循环,我会这样做:

for (int n=index;n<list1.Count;n++) {
    if (list1[n].IndexOf(searchString,StringComparison.OrdinalIgnoreCase)!=0) break;
    listBox1.Items.Add("Index: " + n + " File: " + list1[n]);
}
for (int n=index-1;n>=0;n--) {
    //Do same thing as other loop
}

这将只搜索列表中的两种方式并执行操作,直到它变为无效字符串,然后将打破循环。

相关问题