public static class BubbleSortTool
{
public static void BubbleSort<T>(
this T[] array, AscendingorDescending ascendingorDescending)
where T:IComparable
{
switch (ascendingorDescending)
{
case AscendingorDescending.ascending:
{
while (!
SortCompleteChecK(array, AscendingorDescending.ascending))
{
for (
int i =
0; i < (array.Length -
1); i++
)
{
if (array[i].CompareTo(array[i +
1]) >
0)
{
T temp =
array[i];
array[i] = array[i +
1];
array[i +
1] =
temp;
}
}
}
}
break;
case AscendingorDescending.descending:
{
while (!
SortCompleteChecK(array, AscendingorDescending.ascending))
{
for (
int i =
0; i < (array.Length -
1); i++
)
{
if (array[i].CompareTo(array[i +
1]) <
0)
{
T temp =
array[i];
array[i] = array[i +
1];
array[i +
1] =
temp;
}
}
}
}
break;
default:
break;
}
}
private static bool SortCompleteChecK<T>(T[] array, AscendingorDescending ascendingorDescending)
where T : IComparable
{
switch (ascendingorDescending)
{
case AscendingorDescending.ascending:
{
for (
int i =
0; i < (array.Length-
1); i++
)
{
if (array[i].CompareTo(array[i +
1]) >
0 )
{
return false;
}
}
return true;
}
break;
case AscendingorDescending.descending:
{
for (
int i =
0; i < (array.Length -
1); i++
)
{
if (array[i].CompareTo(array[i +
1]) <
0)
{
return false;
}
}
return true;
}
break;
default:
{ throw new Exception(); }
break;
}
}
public enum AscendingorDescending
{
ascending,descending
}
public static void Print<T>(
this T[] array)
{
StringBuilder sb =
new StringBuilder();
for (
int i =
0; i < array.Length; i++
)
{
sb.Append(array[i]);
sb.Append(" ");
}
Console.WriteLine(sb);
}
public static void Print<T>(
this T[] array,String prompt)
{
StringBuilder sb =
new StringBuilder();
sb.Append(prompt);
for (
int i =
0; i < array.Length; i++
)
{
sb.Append(array[i]);
sb.Append(" ");
}
Console.WriteLine(sb);
}
}
转载于:https://www.cnblogs.com/kotomi/p/8135245.html
相关资源:各显卡算力对照表!