• C# 插入排序


    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;

    namespace HelloWorldApplication
    {
        
        struct KeyType
        {
            public int key;
        };
        struct SqList
        {
            public KeyType[] r;
            public int length;
        };

        class HelloWorld
        {
            static int maxSize = 100;
            private static void InsertSort(SqList l)
            {
                KeyType temp;
              
                for (int i = 1; i < l.length; i++)
                {
                    if (l.r[i].key < l.r[i-1].key)
                    {   // 快速排序
                        temp = l.r[i];
                        l.r[i].key = l.r[i - 1].key;
                        int j = i - 2;
                        for (;j >= 0; j--)
                        {
                            if (temp.key < l.r[j].key)
                            {
                                l.r[j + 1].key = l.r[j].key;
                            }else {
                                // 找到插入位置提前退出循环
                                break;
                            }
                        }
                        l.r[j + 1].key = temp.key;
                       
                    }
                    Console.WriteLine("排序中:l.key:{0}, l.length:{1}", StrSqList(l), l.length);
                }
            }
            private static string StrSqList(SqList l)
            {
                int []a = new int[l.length];
                for(int i=0; i < l.length; i++)
                {
                    a[i] = l.r[i].key;
                }
                return string.Join(",", a);
            }
            static void Main(string[] args)
            {
                /* 我的第一个 C# 程序*/
                // 初始化线性表
                SqList l;
                l.r = new KeyType[maxSize];
                int []r = new int[]{49,38,65,97,76,13,27,49 };
                KeyType key;
                for(int i = 0; i < r.Length; i++)
                {
                    key.key = r[i];
                    l.r[i] = key;
                }
                l.length = r.Length;
                Console.WriteLine("排序前:l.key:{0}, l.length:{1}", StrSqList(l), l.length);
                // 调用排序算法
                InsertSort(l);
                Console.WriteLine("排序后:l.key:{0}, l.length:{1}", StrSqList(l), l.length);
                Console.ReadKey();
            }
        }
    }

  • 相关阅读:
    K8s集群安装Devops
    跨进程通信--共享内存(ashmem)实例
    React+antd实现可编辑单元格,非官网写法,不使用可编辑行和form验证
    前 K 个高频元素
    论文学习——多度量水文时间序列相似性分析
    CH34X-MPHSI高速Master扩展应用—I2C设备调试
    [Acwing] 58周赛 4490. 染色
    举例说明自然语言处理(NLP)技术。
    [原创]jQuery推箱子小游戏(100关且可扩展可选关),休闲,对战,娱乐,小游戏,下载即用,兼容iPad移动端,代码注释全(附源码)
    WIFISKY 7层流控路由器 confirm.php RCE漏洞复现
  • 原文地址:https://blog.csdn.net/pzqingchong/article/details/134483097