c# 从数组中二进制搜索字符串(包括 json)
c# binary search strings from array (including json)
所以我有一个读取字符的 Json 实现,名称进入数组然后我使用 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;
}
您的 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);
您可以看到上面所做的更改反映了如何将默认比较运算符(即“<”、“>”、“==”)替换为 CompareTo
等价运算符。额外的变量 t
是为了避免对对象重复调用 CompareTo
两次。
CompareTo
的工作方式是获取调用对象并将其与传递的对象进行比较。如果传递的对象出现在排序列表中的调用对象之前,方法 returns -1
。如果它出现在后面,它 returns 1
。如果它们相同,则 returns 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
所以我有一个读取字符的 Json 实现,名称进入数组然后我使用 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;
}
您的 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);
您可以看到上面所做的更改反映了如何将默认比较运算符(即“<”、“>”、“==”)替换为 CompareTo
等价运算符。额外的变量 t
是为了避免对对象重复调用 CompareTo
两次。
CompareTo
的工作方式是获取调用对象并将其与传递的对象进行比较。如果传递的对象出现在排序列表中的调用对象之前,方法 returns -1
。如果它出现在后面,它 returns 1
。如果它们相同,则 returns 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