惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
V
Visual Studio Blog
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
L
LangChain Blog
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
P
Proofpoint News Feed
博客园_首页
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - androllen

Python yield 关键词 PDF 构成 桌面客户端的主要类型和技术方案 Python 压缩转义 异步删除嵌套文件夹及文件 数据结构 二叉树遍历 数据结构 线性表 JS 字符串转换为函数体 VSCode 配置内部Python项目 缺少模块ModuleNotFoundError C# 基础知识 算法-数列 C# 设计模式-简单工厂模式 C# 设计模式 C# Utils .Net 异步与同步 WPF StreamGeometry Vue 打开窗口输出文件路径 CSS Flexbox layout 2 CSS Flexbox layout 1
算法-排序算法
androllen · 2025-03-02 · via 博客园 - androllen
    public class SelectionSort
    {
        public static int[] Sort(int[] array)
        {
            int[] copyArray = new int[array.Length];
            Array.Copy(array, copyArray, array.Length);
            // 总共要经过 N-1 轮比较
            for (int i = 0; i < copyArray.Length - 1; i++)
            {
                int min = i;

                // 每轮需要比较的次数 N-i
                for (int j = i + 1; j < copyArray.Length; j++)
                {
                    if (copyArray[min] > copyArray[j])
                    {
                        // 记录目前能找到的最小值元素的下标
                        min = j;
                    }
                }

                // 将找到的最小值和i位置所在的值进行交换
                if (i != min)
                {
                    int tmp = copyArray[i];
                    copyArray[i] = copyArray[min];
                    copyArray[min] = tmp;
                }

            }
            return copyArray;
        }
    }
    public class InsertSort
    {
        public static int[] Sort(int[] array)
        {
            int[] copyArray = new int[array.Length];
            array.CopyTo(copyArray, 0);

            for (int i = 1; i < copyArray.Length; i++)
            {
                int temp = copyArray[i];

                int j = i;

                while (j > 0 && temp < copyArray[j-1])
                {
                    copyArray[j] = copyArray[j-1];
                    j--;
                }

                if (j!=i)
                {
                    copyArray[j]=temp;
                }
            }

            return copyArray;
        }
    }
    public class BubbleSort
    {
        public static int[] _Sort(int[] array)
        {
            int[] copyArray = new int[array.Length];
            array.CopyTo(copyArray, 0);
            for (int i = copyArray.Length - 1; 0 < i; i--)
            {
                for (int j = i - 1; 0 <= j; j--)
                {
                    if (copyArray[j] > copyArray[i])
                    {
                        int temp = copyArray[j];
                        copyArray[j] = copyArray[i];
                        copyArray[i] = temp;
                    }
                }
            }
            return copyArray;
        }

        public static int[] Sort(int[] array)
        {
            int[] copyArray = new int[array.Length];
            array.CopyTo(copyArray, 0);
            for (int i = 1; i < copyArray.Length; i++)
            {
                for (int j = 0; j < copyArray.Length - i; j++)
                {
                    if (copyArray[j] > copyArray[j + 1])
                    {
                        int temp = copyArray[j];
                        copyArray[j] = copyArray[j + 1];
                        copyArray[j + 1] = temp;
                    }
                }
            }
            return copyArray;
        }
    }
    public class Main
    {
        public Main()
        {
            var randomArray = generateArray(15, 3, 29);
            Console.WriteLine(string.Join(',', randomArray));

            var sort = BubbleSort.Sort(randomArray);
            Console.WriteLine("Bubble Sort:" + string.Join(',', sort));

            sort = SelectionSort.Sort(randomArray);
            Console.WriteLine("Selection Sort:" + string.Join(',', sort));

            sort = InsertSort.Sort(randomArray);
            Console.WriteLine("Insert Sort:" + string.Join(',', sort));            

        }


        static int[] generateArray(int length, int mix, int max)
        {
            int[] array = new int[length];
            Hashtable table = new Hashtable();
            Random random = new Random();
            for (int i = 0; table.Count < length; i++)
            {
                var data = random.Next(mix, max);
                if (!table.ContainsKey(data) && data != 0)
                {
                    table.Add(data, data);
                    array[table.Count - 1] = data;
                }
            }
            return array;
        }

    }