public class MySortClass<T> where T:IComparable
{
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="array"></param>
public void Sort(T[] array)
{
for (int j = 0; j < array.Length; j++)
{
for (int i = j; i < array.Length; i++)
{
T temp = default(T);
if (array[j].CompareTo(array[i]) > 0)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Print(array);
}
/// <summary>
/// 直接插入排序
/// </summary>
/// <param name="array"></param>
public void Sort2(T[] array)
{
for (int i = 0; i < array.Length-1; i++)
{
//前一个小
if (array[i+1].CompareTo(array[i])<0)
{
int j = 0;
T temp = array[i + 1];
System.Diagnostics.Debug.WriteLine("tangshu:{0},value:{1}",i,temp);
for (j = i; j >= 0 && temp.CompareTo(array[j]) < 0; --j)
{
//向后移动数
System.Diagnostics.Debug.WriteLine(array[j + 1]);
array[j + 1] = array[j];
}
array[j+1] = temp;
}
}
Print(array);
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="array"></param>
public void Sort3(T[] array)
{
for (int j = array.Length - 1; j >= 0; j--)
{
for (int i = 0; i < j; i++)
{
if (array[i + 1].CompareTo(array[i]) < 0)
{
T temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
}
}
}
Print(array);
}
public void Print(T[] array)
{
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
}
}
{
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="array"></param>
public void Sort(T[] array)
{
for (int j = 0; j < array.Length; j++)
{
for (int i = j; i < array.Length; i++)
{
T temp = default(T);
if (array[j].CompareTo(array[i]) > 0)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Print(array);
}
/// <summary>
/// 直接插入排序
/// </summary>
/// <param name="array"></param>
public void Sort2(T[] array)
{
for (int i = 0; i < array.Length-1; i++)
{
//前一个小
if (array[i+1].CompareTo(array[i])<0)
{
int j = 0;
T temp = array[i + 1];
System.Diagnostics.Debug.WriteLine("tangshu:{0},value:{1}",i,temp);
for (j = i; j >= 0 && temp.CompareTo(array[j]) < 0; --j)
{
//向后移动数
System.Diagnostics.Debug.WriteLine(array[j + 1]);
array[j + 1] = array[j];
}
array[j+1] = temp;
}
}
Print(array);
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="array"></param>
public void Sort3(T[] array)
{
for (int j = array.Length - 1; j >= 0; j--)
{
for (int i = 0; i < j; i++)
{
if (array[i + 1].CompareTo(array[i]) < 0)
{
T temp = array[i + 1];
array[i + 1] = array[i];
array[i] = temp;
}
}
}
Print(array);
}
public void Print(T[] array)
{
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
}
}