冒泡排序 C#
static void Main(string[] args)
{
int[] nums = { 5, 10, 3,8,20,7};
Console.WriteLine("数组长度:"+nums.Length);
for (int i = 0; i < nums.Length; i++) //外层循环,控制次数
{
for (int j = 0; j < nums.Length-i-1; j++) //内层循环,相临2个数比较
{
int temp;
if (nums[j] > nums[j + 1])
{
temp = nums[j + 1];
nums[j + 1] = nums[j];
nums[j] = temp;
}
}
}
for (int i = 0; i < nums.Length; i++)
{
Console.WriteLine(nums[i]);
}
Console.ReadKey();
}