• 希尔排序C#


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

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

        class Demo
        {
            static int maxSize = 100;
            private static void BInsertSort(SqList l)
            {
                KeyType temp;
                Console.WriteLine("排序前:l.key:{0}, l.length:{1}", StrSqList(l), l.length);
                for (int i = 1; i < l.length; i++)
                {
                    temp = l.r[i];
                    int low = 0;
                    int high =  i - 1;
                    while (low <= high)
                    {
                        int m = (low + high) / 2;
                        if (temp.key < l.r[m].key)
                        {
                            high--;
                        } else
                        {
                            low++;
                        }
                    }
                    for (int j = i - 1; j >= high + 1; j--)
                    {
                        l.r[j + 1].key = l.r[j].key;
                    }
                    l.r[high + 1].key = temp.key;
                    // Console.WriteLine("排序中:l.key:{0}, l.length:{1}", StrSqList(l), l.length);
                }
                Console.WriteLine("排序后:l.key:{0}, l.length:{1}", StrSqList(l), l.length);
            }

            private static void ShellInsert(SqList l, int dk)
            {
                // l 按照增量dk 插入排序
                // 希尔插入排序算法
                KeyType temp;
               
                for(int i = dk; i < l.length; i++)
                {
                    if (l.r[i].key < l.r[i-dk].key)
                    {
                        temp = l.r[i];
                        l.r[i].key = l.r[i - dk].key;
                        int j = i - 2 * dk;
                        for (; j >= 0; j-=dk)
                        {
                            if (temp.key < l.r[j].key) {
                                l.r[j + 1].key = l.r[j].key;
                            }
                            else
                            {
                                break;
                            }
                        }
                        l.r[j+dk].key = temp.key;
                    }
                }
              

            }
            private static void ShellSort(SqList l)
            {
           
                Console.WriteLine("排序前:l.key:{0}, l.length:{1}", StrSqList(l), l.length);
                ShellInsert(l, 5);
                Console.WriteLine("第一趟排序后:l.key:{0}, l.length:{1}", StrSqList(l), l.length);
                ShellInsert(l, 3);
                Console.WriteLine("第二趟排序后:l.key:{0}, l.length:{1}", StrSqList(l), l.length);
                ShellInsert(l, 1);
                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,55,4 };
                KeyType key;
                for(int i = 0; i < r.Length; i++)
                {
                    key.key = r[i];
                    l.r[i] = key;
                }
                l.length = r.Length;

                // 调用排序算法
                ShellSort(l);
                
                Console.ReadKey();
            }
        }
    }

  • 相关阅读:
    2023年09月 C/C++(二级)真题解析#中国电子学会#全国青少年软件编程等级考试
    使用ChatGPT高效完成简历制作[中篇]-有爱AI实战教程(五)
    蓝桥杯每日一题2023.10.1
    spring framework 5.2 文档 - 概述
    Prometheus(一)——概述、监控体系、生态组件、部署
    R语言替换字符串中指定字符的子串:sub函数查找字符串中第一个匹配到的子串并替换(第一个之后的子串不做替换)
    背课文记单词,读课文记单词,读文章记单词;40篇文章搞定3500词;71篇文章突破中考单词;15篇文章贯通四级词汇;15篇文章贯通六级词汇
    RabbitMQ简介
    嵌入式开发--Altium技巧:原理图设置
    Java小技巧——对象打印之重写toString方法
  • 原文地址:https://blog.csdn.net/pzqingchong/article/details/134484293