C#冒泡排序算法

用了两种形式的数据,一个是泛型List,一个是数据int[]。记录一下,作为自己学习过程中的笔记。

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

namespace 冒泡排序算法
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> _list = new List<int>() { 29, 20, 49, 84, 3, 48, 86, 95, 69, 28 };
            Bubble(_list);
            foreach (var item in _list)
            {
                Console.WriteLine(item);
            }
            int[] bortargs = { 29, 20, 49, 84, 3, 48, 86, 95, 69, 28 };
            Bubble(bortargs);
            foreach (var article in bortargs)
            {
                Console.WriteLine(article);
            }
            Console.ReadKey();
        }
        static void Bubble(List<int> list)
        {
            int temp = 0;
            for (int i = list.Count; i > 0; i--)
            {
                for (int j = 0; j < i - 1; j++)
                {
                    if (list[j] > list[j + 1])
                    {
                        temp = list[j];
                        list[j] = list[j + 1];
                        list[j + 1] = temp;
                    }
                }
            }
        }

        static void Bubble(int[] args)
        {
            int temp = 0;
            for (int i = args.Length; i > 0; i--)
            {
                for (int j = 0; j < i - 1; j++)
                {
                    if (args[j] > args[j + 1])
                    {
                        temp = args[j];
                        args[j] = args[j + 1];
                        args[j + 1] = temp;
                    }
                }
            }
        }

    }
}
View Code
原文地址:https://www.cnblogs.com/zuimeideshi520/p/6025402.html