最简单的排序方式,实现如下:
代码
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="data"></param>
public static void BubbleSort(int[] data)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
int temp;
int index = data.Length - 1;
while (index > 0)
{
for (int i = 0; i < index; i++)
{
if (data[i] > data[i + 1])
{
temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
}
--index;
}
}
/// 冒泡排序
/// </summary>
/// <param name="data"></param>
public static void BubbleSort(int[] data)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
int temp;
int index = data.Length - 1;
while (index > 0)
{
for (int i = 0; i < index; i++)
{
if (data[i] > data[i + 1])
{
temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
}
--index;
}
}