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

系列文章目录

C#知识点



👉前言

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


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

👉一、冒泡排序

👉1-1 介绍

重复遍历数组比较相邻的元素,如果前面的比后面的大,就交换它们两个
每次遍历整个数组,遍历完成后,下一次遍历的索引减一(范围往左缩一位)
持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较,则排序完成

👉1-2 动态展示效果

在这里插入图片描述

👉1-3 算法代码如下

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

public class Bubble_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;    // 得到数组的长度     
        for (int i = 0; i < n; i++)
        {
            bool flag = false;      // 表示本轮是否有进行变量交换
            for (int idx = 0; idx < n - i - 1; idx++)
            {
                if (nums[idx] > nums[idx + 1])
                {
                    int temp = nums[idx];
                    nums[idx] = nums[idx + 1];
                    nums[idx + 1] = temp;
                    flag = true;
                }
            }
            // 如果flag为False,那说明本轮排序没有进行任何变量交换
            // 数组已经是有序的了
            if (!flag)
            {
                break;
            }
        }
    }
}

👉1-4 运行结果如下

在这里插入图片描述

👉二、选择排序

👉2-1 介绍

数组的初始状态:有序区为空,无序区为[0,…,n]
每次找到无序区里的最小元素,添加到有序区的最后
重复前面步骤,直到整个数组排序完成

👉2-2 动态展示效果

在这里插入图片描述

👉2-3 算法代码如下

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

public class Selection_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;
        for (int i = 0; i < n; i++)
        {
            // 找无序区中最小的元素 
            int minIndex = i;  //无序区中的最小元素的索引
            for (int j = i + 1; j < n; j++)
            {
                if (nums[j] < nums[minIndex])
                {
                    minIndex = j;
                }
            }
            // 执行完上面的循环后
            // minIndex就是无序区中的最小元素的索引
            // 把最小元素和有序区的后一个元素交换位置
            int temp = nums[i];
            nums[i] = nums[minIndex];
            nums[minIndex] = temp;
        }
    }
}

👉运行效果如下

在这里插入图片描述

👉三、随机快速排序

👉3-1 介绍

将一个待排序的数组随机选择“基准”(pivot)分割成两个独立的子数组
其中一部分的所有元素都比另外一部分的所有元素都要小
按此方法对这两部分元素分别进行快速排序
整个排序过程可以递归进行,以此达到整个数据变成有序序列

👉3-2 动态展示效果

在这里插入图片描述

👉3-3 算法代码如下

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

public class RandomSpeed_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()
    {
        quickSort(test, 0, test.Length - 1);
    }
    // 随机分区 
    public int RandomPartition(int[] nums, int left, int right)
    {
          // 随机生成数 
        int i = Random.Range(left, right);
        int temp = nums[i];
        nums[i] = nums[right];
        nums[right] = nums[i];

        return partition(nums, left, right);
    }

    // 分区 
    public  int partition(int[] nums, int left, int right)
    {
        int pivot = nums[left];     // 区域的第一个元素作为基准值

        while (left < right)
        {
            while (left < right && nums[right] > pivot)
                right--;
            nums[left] = nums[right];

            while (left < right && nums[left] <= pivot)
                left++;
            nums[right] = nums[left];
        }
        nums[right] = pivot;        // 基准值的正确位置 

        return left;        // 返回基准值 
    }

    // 快速排序 
    public  void quickSort(int[] nums, int left, int right)
    {
        if (left >= right)
            return;

        // 分区 --> 分好区之后的基准值的索引 
        int pivotIndex = RandomPartition(nums, left, right);
        // 左边的区域, left -> pivotIndex - 1 
        quickSort(nums, left, pivotIndex - 1);
        // 右边的区域,pivotIndex+1->right 
        quickSort(nums, pivotIndex + 1, right);
    }
}

👉3-4 运行效果如下

在这里插入图片描述

👉四、插入排序

👉4-1 介绍

整个数组分为左边部分有序元素集合和右边部分无序元素集合
一开始从索引为一的元素开始逐渐递增与有序集合进行比较
将未排序的元素一个一个地插入到有序的集合中,插入时把所有有序集合从后向前扫一遍,找到合适的位置插入

👉4-2 动态展示效果

在这里插入图片描述

👉4-3 算法代码如下

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

public class Insertion_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;     // 数组的长度

        for (int i = 0; i < n - 1; i++)
        {
            int curNum = nums[i + 1];     // 无序区的第一个元素 
            int idx = i;    //有序区的最后一个元素的索引

            while (idx >= 0 && nums[idx] > curNum)
            {
                nums[idx + 1] = nums[idx];  // 把有序区的元素往后挪一位
                idx -= 1;
            }
            nums[idx + 1] = curNum;
        }
    }
   
}

👉4-4 运行结果如下

在这里插入图片描述

👉壁纸分享

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


👉总结

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

心疼你的一切

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

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

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

打赏作者

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

抵扣说明:

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

余额充值