C#冒泡法排序算法實例分析
更新時間:2015年07月03日 14:43:51 作者:不是JS
這篇文章主要介紹了C#冒泡法排序算法,結合兩個常用實例分析了C#冒泡排序算法的相關實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了C#冒泡法排序算法。分享給大家供大家參考。具體實現(xiàn)方法如下:
static void BubbleSort(IComparable[] array)
{
int i, j;
IComparable temp;
for (i = array.Length - 1; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (array[j].CompareTo(array[j + 1]) > 0)
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
泛型版本:
static void BubbleSort<T>(IList<T> list) where T : IComparable<T>
{
for (int i = list.Count - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
IComparable current = list[j];
IComparable next = list[j + 1];
if (current.CompareTo(next) > 0)
{
list[j] = next;
list[j + 1] = current;
}
}
}
}
希望本文所述對大家的C#程序設計有所幫助。

