C#经典排序算法总结(二)

系列文章目录

C#知识点



👉前言

今天介绍一下经典的排序算法,代码全是C#写的,如需要其他语言的写法,请自行百度
上上一篇写了三种最快的排序算法
上一篇写了四种排序算法
这一篇写其他的排序算法
接下来就来一一介绍一下吧,以下排序运用场景是unity,如果不是别忘了修改修改哦
大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。
欢迎点赞评论哦.
下面就让我们进入正文吧 !


提示:以下是本篇文章正文内容,下面案例可供参考

👉一、计数排序

👉1-1 介绍

找出待排序的数组中最大和最小的元素
统计数组中每个值为i的元素出现的次数,存入数组C的第i项
对所有的计数累加(从C中的第一个元素开始,每一项和前一项相加)
向填充目标数组:将每个元素i放在新数组的第C(i)项,每放一个元素就将C(i)减去1

👉1-2 动态展示效果

在这里插入图片描述

👉1-3 算法代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Counting_Sort : MonoBehaviour
{
    public int[] test = { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
    // Start is called before the first frame update
    void Start()
    {
        SortRealize(test);
    }

    public void SortRealize(int[] nums)
    {
        // 长度小于等于0直接return
        if (nums.Length <= 0)
            return;

        int min = nums[0];
        int max = min;
        // 找出数组最大元素和最小元素 
        foreach (int item in nums)
        {
            if (item > max)
            {
                max = item;
            }
            else if (item < min)
            {
                min = item;
            }
        }

        // 把所有元素存入counting数组 
        int[] counting = new int[max - min + 1];
        for (int i = 0; i < nums.Length; i++)
        {
            counting[nums[i] - min] += 1;
        }

        int index = -1;
        for (int i = 0; i < counting.Length; i++)
        {
            for (int j = 0; j < counting[i]; j++)
            {
                index++;
                nums[index] = i + min;
            }
        }
    }
}

👉1-4 运行结果如下

在这里插入图片描述

👉二、基数排序

👉2-1 介绍

基数排序是一种非比较型排序和利用桶的算法,直接利用每个位数作为下标放入桶中,无需与其他元素比较大小
将待比较的数字从较低位到较高位依次放入桶中, 再按照桶的顺序取出
直到最长位数的数字被完全比较,即可得到已排序的数组

👉2-2 动态展示效果

在这里插入图片描述

👉2-3 算法代码如下

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Radix_Sort : MonoBehaviour
{
    public int[] test = { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
    // Start is called before the first frame update
    void Start()
    {
        SortRealize(test);
    }

    public void SortRealize(int[] nums,int bucketNum=10)
    {
        int maxLength = MaxLength(nums);

        //创建bucket时,在二维中增加一组标识位,
        //其中bucket[x, 0]表示这一维所包含的数字的个数
        //通过这样的技巧可以少写很多代码
        int[,] bucket = new int[bucketNum, nums.Length + 1];

        for (int i = 0; i < maxLength; i++)
        {
            foreach (var num in nums)
            {
                int bit = (int)(num / Math.Pow(10, i) % 10);
                bucket[bit, ++bucket[bit, 0]] = num;
            }

            for (int count = 0, j = 0; j < bucketNum; j++)
            {
                for (int k = 1; k <= bucket[j, 0]; k++)
                {
                    nums[count++] = bucket[j, k];
                }
            }

            //最后要重置这个标识
            for (int j = 0; j < bucketNum; j++)
            {
                bucket[j, 0] = 0;
            }

        }
    }

    private static int MaxLength(int[] array)
    {
        if (array.Length <= 0)
            return 0;

        int max = array.Max();      // 取出数组的最大值 
        return (int)Math.Log10(max) + 1;    // 取出位数 
    }
}

👉运行效果如下

在这里插入图片描述

👉三、桶排序

👉3-1 介绍

桶排序是计数排序的升级版
根据数组的最大值与最小值申请一些桶(生成对应的序列)
将数组的元素放入桶中,并保证桶里是有序的
合并每个桶,得到的就是一个有序的序列

👉3-2 动态展示效果

在这里插入图片描述

👉3-3 算法代码如下

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Bucket_Sort : MonoBehaviour
{
    public int[] test = { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
    // Start is called before the first frame update
    void Start()
    {
        SortRealize(test);
    }

    public void SortRealize(int[] nums, int bucketsize = 5)
    {
        int max = nums.Max(), min = nums.Min();               // 最大值与最小值
        int bucketnums = (max - min) / bucketsize + 1;           // 分配的桶数量

        List<List<int>> buckets = new List<List<int>>();
        // 生成桶
        for (int i = 0; i < bucketnums; i++)
        {
            buckets.Add(new List<int>());
        }

        //将数组的元素放入桶中,并保证桶里是有序的        
        for (int i = 0; i < nums.Length; i++)
        {
            int bucketIndex = (nums[i] - min) / bucketsize;
            buckets[bucketIndex].Add(nums[i]);
        }

        int index = 0;
        for (int i = 0; i < buckets.Count; i++)
        {
            buckets[i].Sort();      // 对生成的每个桶排序 
            for (int j = 0; j < buckets[i].Count; j++)
            {
                nums[index++] = buckets[i][j];
            }
        }
    }
}

👉3-4 运行效果如下

在这里插入图片描述

👉四、希尔排序

👉4-1 介绍

希尔排序是插入排序的优化版本
设定一个增量gap,将数组按照gap分组
依次对每一组进行插入排序
缩小增量gap,重复前两个步骤,直到gap缩小到一,那么最后一次排序就是
插入排序

👉4-2 动态展示效果

在这里插入图片描述

👉4-3 算法代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hill_Sort : MonoBehaviour
{
    public int[] test = { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
    // Start is called before the first frame update
    void Start()
    {
        SortRealize(test);
    }
   
    public void SortRealize(int[] nums)
    {
        int n = nums.Length;    // 数组的长度 
        int gap = n / 2;    // 设定一个增量gap 

        while (gap >= 1)
        {
            // 分组
            for (int i = gap; i < n; i++)
            {
                int curNum = nums[i];   // 当前要插入的无序区的元素的值
                int idx = i - gap;      // 当前元素所在小组的有序区的最后一个元素的索引 
                while (idx >= 0 && curNum < nums[idx])      // 插入排序
                {
                    nums[idx + gap] = nums[idx];
                    idx -= gap;
                }
                nums[idx + gap] = curNum;
            }
            gap /= 2;   // 缩小增量 
        }
    }
}

👉4-4 运行结果如下

在这里插入图片描述
最终的总结
在这里插入图片描述

👉壁纸分享

请添加图片描述
请添加图片描述


👉总结

本次总结的就是另外的四种经典排序的算法,有需要会继续添加新的排序算法
如能帮助到你,就帮忙点个赞吧,三连更好哦,谢谢
你的点赞就是对博主的支持,有问题记得留言评论哦!
不定时更新Unity开发技巧,觉得有用记得一键三连哦。么么哒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心疼你的一切

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值