首页 > 代码库 > 数组冒泡排序算法解析
数组冒泡排序算法解析
/*
* 冒泡排序算法
*/
int[] numbers = { 23, 24, 44, 21, 94, 12, 26, 09 };
for (int i = 0; i < numbers.Length; i++)
{
for (int j = i + 1; j < numbers.Length; j++)
{
if (numbers[j] < numbers[i])
{
ChangeNumber(numbers[j], numbers[i]);
}
}
}
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
Console.ReadKey();
}
/*
* 变量互换
*/
static void ChangeNumber(int number1, int number2)
{
number1 = number1 + number2;
number2 = number1 - number2;
number1 = number1 - number2;
}
数组冒泡排序算法解析