汎型の泡、挿入、選択、ヒルアルゴリズム
8646 ワード
自分のために残しておこう...
間違いがあったら指摘してほしい...ありがとう
これは主に見ました.Netの美が突発的に奇想天外に書いたもの
参照先:
http://www.cnblogs.com/JimmyZhang/category/128644.html
public class FanXingHelper<T> where T:IComparable
{
/// <summary>
///
/// </summary>
/// <param name="array"></param>
public T[] MaoPao(T[] array) {
int length = array.Length;
for (int i = 0; i <= length-2; i++)
{
for (int j = length-1; j>=1 ; j--)
{
if (array[j].CompareTo(array[j-1])<0)
{
T temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
return array;
}
/// <summary>
///
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public T[] ZhiJieChaRu(T[] array) {
int length = array.Length;
for (int i = 1; i < length-1; i++)
{
int j = i;
while (j>=1&&array[j].CompareTo(array[j-1])>0)
{
T temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
j--;
}
}
return array;
}
/// <summary>
///
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public T[] XuanZhePaiXu(T[] array) {
int length = array.Length;
for (int i = 0; i <= length-2; i++)
{
int index = i;
for (int j = length-1; j >i; j--)
{
if (array[j].CompareTo(array[index])<0)
{
index = j;
}
T temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
return array;
}
/// <summary>
///
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public T[] XiErPaiXu(T[] array) {
int length = array.Length;
for (int i = length/2;i>0; i=i/2)
{
for (int j = i; j < i; j++)
{
if (array[j].CompareTo(array[j-i])>0)
{
T temp = array[j];
int key = j - i;
while (key >= 0 && array[key].CompareTo(temp) > 0) {
array[key + i] = array[key];
key -= i;
}
array[key + i] = temp;
}
}
}
return array;
}
}
間違いがあったら指摘してほしい...ありがとう
これは主に見ました.Netの美が突発的に奇想天外に書いたもの
参照先:
http://www.cnblogs.com/JimmyZhang/category/128644.html