【C#】实现冒泡排序输出

排序内容:

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);
        }
    }
}

运行结果:

发布了62 篇原创文章 · 获赞 14 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Yanzudada/article/details/102796659
今日推荐