以前每次想做個查找算法都專門寫個,於是搞了這個泛型類,哈哈。查找類一般有排序類一起使用,等會再發一個泛型排序類上來。
using System;
using System.Collections.Generic;
using System.Text;
namespace CommonLibrary
...{
/**//// <summary>
/// Finder 查找類
/// </summary>
public class Finder
...{
/**//// <summary>
/// 兩個值的比較委托
/// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="value1">值1</param>
/// <param name="value2">值2</param>
/// <returns>返回值,值1大於值2返回1,值1小於值2返回-1,值1等於值2返回0</returns>
public delegate int Compare<T>(T value1, T value2 /**//// <summary>
/// 把一個值插入到一個有序的集合
/// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="myList">目標集合</param>
/// <param name="insertValue">要插入的值</param>
/// <param name="myCompareMethod">兩個值的比較方法</param>
public static void InsertToSort<T>(IList<T> myList, T insertValue, Compare<T> myCompareMethod)
...{
int place = FindInsertPlace<T>(myList, insertValue, 0, myList.Count, myCompareMethod);
myList.Insert(place, insertValue);
}
/**//// <summary>
/// 是否存在此值
/// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="myList">目標集合</param>
/// <param name="inputKey">要檢查的值</param>
/// <param name="myCompareMethod">兩個值的比較方法</param>
/// <returns>返回值</returns>
public static bool Contains<T>(IList<T> myList, T inputKey, Compare<T> myCompareMethod)
...{
int place = FindPlace<T>(myList, inputKey, 0, myList.Count, myCompareMethod);
if (place == -1)
...{
return false;
}
else
...{
return true;
}
}
/**//// <summary>
/// 二分查找法 /// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="myList">目標集合</param>
/// <param name="inputKey">要查找的值</param>
/// <param name="myCompareMethod">兩個值的比較方法</param>
/// <returns>值的索引,未找到返回-1</returns>
public static int FindPlace<T>(IList<T> myList, T inputKey, Compare<T> myCompareMethod)
...{
return FindPlace<T>(myList, inputKey, 0, myList.Count, myCompareMethod);
}
/**//// <summary>
/// 二分查找法
/// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="myList">目標集合</param>
/// <param name="inputKey">要查找的值</param>
/// <param name="start">起始位置</param>
/// <param name="end">結束位置</param>
/// <param name="myCompareMethod">兩個值的比較方法</param>
/// <returns>值的索引,未找到返回-1</returns>
public static int FindPlace<T>(IList<T> myList, T inputKey, int start, int end, Compare)
...{
if (myList.Count == 0) return -1;
int nowplace = (int)((start + end) / 2);
if (start == nowplace)
...{
T nowvalue = myList[nowplace];
if (myCompareMethod(nowvalue, inputKey) == 0)
...{
return nowplace;
}
else
...{
return -1;
}
}
else
...{
T nowvalue = myList[nowplace];
if (myCompareMethod.Invoke(nowvalue, inputKey) == 1)
...{
return FindPlace(myList, inputKey, start, nowplace, myCompareMethod);
}
else if (myCompareMethod.Invoke(nowvalue, inputKey) == -1)
...{
return FindPlace(myList, inputKey, nowplace, end, myCompareMethod);
}
else
...{
return nowplace;
}
}
}
/**//// <summary>
/// 查找值的插入位置
/// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="myList">目標集合</param>
/// <param name="inputKey">要查找的值</param>
/// <param name="myCompareMethod">兩個值的比較方法</param>
/// <returns>插入位置</returns>
public static int FindInsertPlace<T>(IList<T> myList, T inputKey, Compare<T> myCompareMethod)
...{
return FindInsertPlace<T>(myList, inputKey, 0, myList.Count, myCompareMethod);
}
/**//// <summary>
/// 查找值的插入位置
/// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="myList">目標集合</param>
/// <param name="inputKey">要查找的值</param>
/// <param name="start">起始位置</param>
/// <param name="end">結束位置</param>
/// <param name="myCompareMethod">兩個值的比較方法</param>
/// <returns>插入位置</returns>
public static int FindInsertPlace<T>(IList<T> myList, T inputKey, int start, int end, Compare<T> myCompareMethod)
...{
if (myList.Count == 0) return 0;
int nowplace = (start + end) / 2;
if (start == nowplace)
...{
T nowvalue = myList[nowplace];
if (myCompareMethod.Invoke(nowvalue, inputKey) == 1)
...{
&nb return start;
}
else
...{
return end;
}
}
else
...{
T nowvalue = myList[nowplace];
if (myCompareMethod.Invoke(nowvalue, inputKey) == 1)
...{
return FindInsertPlace(myList, inputKey, start, nowplace, myCompareMethod);
}
else
...{
return FindInsertPlace(myList, inputKey, nowplace, end, myCompareMethod);
}
}
}
}
}