C#算法之冒泡排序
2021-04-24 16:29
                         标签:第一个   ati   HERE   冒泡   rabl   using   static   code   generic    排序规则:   比较相邻的元素。如果第一个比第二个大,就交换它们两个。   对每对相邻元素做同样的工作,从开始第一对到最后一对。这步做完之后,最后的元素会是最大的数。   针对所有的元素重复以上的步骤,除了最后一个。   持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要进行比较。 时间复杂度:O(n^2) 泛型冒泡排序   C#算法之冒泡排序 标签:第一个   ati   HERE   冒泡   rabl   using   static   code   generic    原文地址:https://www.cnblogs.com/sy-liu/p/13262498.htmlusing System;
using System.Collections.Generic;
using System.Text;
namespace 排序算法
{
    class BubbleSort
    {
        public static void Sort(int[] arr)
        {
            int n = arr.Length;
            for(int i = 0; i )
            {
                // 减去i不需要对已经排好的元素再次进行比较
                for (int j = 0; j 1 - i; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        Swap(arr, j, j + 1);
                    }
                }
            }
        }
        private static void Swap(int[] arr, int i,int j)
        {
            int t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 排序算法
{
    class BubbleSortGeneric
    {
        public static void Sort
下一篇:Python语法的使用和简介