本文只是陈列了最基本的代码,并没有阐述原理,还望读者自己探索。
代码展示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01_冒泡排序
{
class Program
{
static void Main(string[] args)
{
int[] nums = new int[] { 2, 34, 456, 444440, 3432, 34325 };
int temp = 0;
// 外循环表示循环的趟数
for (int j = 0; j < nums.Length; j++)
{
// 内循环表示冒泡的处理
for (int i = 0; i < nums.Length-j-1; i++)
{
// 通过比较大小,实现元素交换
if (nums[i]>nums[i+1])
{
temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
}
}
}
// 利用 for 循环将元素逐个显示出来
for (int i = 0; i < nums.Length; i++)
{
Console.WriteLine(nums[i]+ "\t");
}
Console.ReadKey();
}
}
}