【C#】实现冒泡排序输出
程序员文章站
2024-01-03 11:26:58
...
排序内容:
var db = new double[] { 20.3, 11.4, 33.1, 45.2, 50.1, 60.5 };
实现代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace day01
{
class BubbleSort
{
public static double[] Bubble(double[] db)
{
for (int i = 0; i < db.Length; i++)
{
for (int j = i + 1; j < db.Length; j++)
{
if (db[i] > db[j])
{
db[j] = db[i] + db[j];
db[i] = db[j] - db[i];
db[j] = db[j] - db[i];
}
}
}
return db;
}
public static void Print(double[] db)
{
for (int i = 0; i < db.Length; i++)
{
Console.WriteLine(db[i]);
}
Console.ReadKey();
}
static void Main(string[] args)
{
//数据源
var db = new double[] { 20.3, 11.4, 33.1, 45.2, 50.1, 60.5 };
//冒泡排序
db = Bubble(db);
//打印
Print(db);
}
}
}
运行结果: