//定义一个数组
int[] numbers = { 1, 36, 5, 6, 618, 8, 9, 3, 34, 33, 4 };
//冒泡排序口诀:forfor,外层长度-1,内层长度-1-i
for (int i = 0; i < numbers.Length - 1; i++)
{
for (int j = 0; j < numbers.Length - 1 - i; j++)
{
if (numbers[j] > numbers[j + 1])
{
//交换变量
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
//遍历数组
foreach (var item in numbers)
{
Console.WriteLine(item);
}
|