流浪のwolf

卷帝

导航

dotnet 冒泡排序

// See https://aka.ms/new-console-template for more information
using ConsoleApp1;

Console.WriteLine("Hello, World!");
// 我委托你办事情,作为委托方只要满足被委托方的规则的事情(也就是方法),他都可以帮我解决,我需要给它提供金钱(也就是参数)。
// 总结:就是一些常用(公用的代码业务)委托给别人定制化(指定化)开发。做到代码的灵活性。

// 泛型:他就是把类型做到了通用。
// 反射:就是操作dll文件的帮助类库。帮我们找到dll文件里面的各种信息;
// 特性:特性是让我们的类,方法,属性,参数之类在不修改源码的情况下的产生了更多功能,或者其他的功能(打一个标签就产生了新功能,本质就是我们AOP编程的另一种实现方式)。
int[] items = new int[] { 1, 23, 345, 6, 2345 };
Console.WriteLine(items);
Sort sort = new Sort();
sort.BubbleSort(items);

// 打印结果
foreach (int k in items)
{
    Console.WriteLine(k);
}

// 冒泡函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class Sort
    {
        // 冒泡排序
        public void BubbleSort(int[] items)
        {
            int i;
            int j;
            int temp;
            if (items == null)
            {
                return;
            }
            for (i = items.Length - 1; i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    if (items[j - 1] > items[j])
                    {
                        // 交换位置
                        temp = items[j - 1];
                        items[j - 1] = items[j];
                        items[j] = temp;
                    }
                }
            }
        }
    }
}

 

posted on 2024-05-04 19:07  流浪のwolf  阅读(1)  评论(0编辑  收藏  举报