• 希尔排序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();
            }
        }
    }

  • 相关阅读:
    (附源码)springboot宠物领养系统 毕业设计 241104
    凡人修仙传之工作试用期篇
    STM32F10x SPL V3.6.2 集成 FreeRTOS v202112
    C#读取CAD文件(dwg/dxf)并处理
    SpringBoot如何集成Log模块呢?
    三阶段面试题——vue
    C Primer Plus(6) 中文版 第12章 存储类别、链接和内存管理 12.1 存储类别
    每天分享一点运维小知识---pm2工具
    LeetCode 557. 反转字符串中的单词 III
    java毕业生设计医院患者管理系统计算机源码+系统+mysql+调试部署+lw
  • 原文地址:https://blog.csdn.net/pzqingchong/article/details/134484293