c#二进制搜索数组中的字符串(包括json)

时间:2016-12-17 14:24:44

标签: c# binary-search

所以我有一个Json implimentation读取字符,名称进入数组然后我使用 Array.BinarySearch 来获取元素的位置。

我正在研究如何利用我自己的二进制搜索。我在逻辑上看到如何处理为搜索输入的字符串名称时遇到了麻烦。

我没有使用Array.BinarySearch,而是需要一个单独的算法方法。

任何建议/策略? :)

示例:

       /* json array implimented, manu printed etc... before this point,      */ 



   static void FindCharacters(Characters[] characters)
    {
        Characters result = new Characters();

        string userInput = Console.ReadLine();

        string[] name = new string[10000];



        Console.Write("Search Name : ");
        string searchKeyword = Console.ReadLine();

        if (userInput.ToLower() == "name")
        {

            name = characters.Select(m => m.Name).ToArray();   

        Array.Sort(name);
        Sorting.Sort(characters, searchKeyword);


        var tmp = BinarySearch(name, searchKeyword); 

        if (tmp < 0)
        {
            Console.WriteLine("No data found!");
            return;
        }
        else
        {
            result = characters[tmp];

            CharacterPrint(result);
        }
            //result = characters[tmp];  //Convert.ToInt32(tmp)
            //CharacterPrint(result);

}

    public static int BinarySearch(int[] name, int item)
    {

        int min = 0;
        int N = name.Length;
        int max = N - 1;
        do
        {
            int mid = (min + max) / 2;
            if (item > name[mid])
                min = mid + 1;
            else
                max = mid - 1;
            if (name[mid] == item)
                return mid;
            //if (min > max)
            //   break;
        } while (min <= max);
        return -1;
    }

1 个答案:

答案 0 :(得分:0)

您的int解决方案对字符串完全正常。实际上,通过调整几行,它可以用于实现IComparable的任何数据类型:

public static int BinarySearch<T>(T[] name, T item) 
    where T : IComparable<T>
{

    int min = 0;
    int N = name.Length;
    int max = N - 1;
    do
    {
        int mid = (min + max) / 2;
        int t = item.CompareTo(name[mid]);      // Temp value holder
        if (t > 0)                              // item > name[mid]
            min = mid + 1;
        else if (t < 0)                         // item < name[mid]
            max = mid - 1;
        else                                    // item == name[mid]
            return mid;
        //if (min > max)
        //   break;
    } while (min <= max);
    return -1;
}

您可以这样称呼它:

string[] names = // ...
string name = //...

// Explicit calling
int idx = BinarySearch<string>(names, name);

// Implicit calling
// The following option works because the C# compiler can tell you are
// using two values of type string and inserts the correct generic
// type for you
int idx = BinarySearch(names, name);

您可以看到上面所做的更改反映了如何替换默认比较运算符(即&#34;&lt;&#34;,&#34;&gt;&#34;,&#34; ==&# 34;)与他们的CompareTo等价物。额外的变量t可以避免在对象上冗余地调用CompareTo两次。

CompareTo的工作方式是它接受调用对象并将其与传递的对象进行比较。如果传递的对象出现在排序列表中的调用对象之前,则该方法返回-1。如果它出现在之后,则返回1。如果它们相同,则返回0

有关此内容的说明,请参阅以下示例:

// The following values are compared based on standard lexical alphabetical order

a.CompareTo(b); // "b" would appear after "a", so this returns 1
c.CompareTo(b); // "b" would appear before "c", so this returns -1
b.CompareTo(b); // "b" and "b" are the same value, so this returns 0