当前位置: 首页 > news >正文

C# 十大排序算法

以下是常见的十大排序算法(按照学习和实现的顺序排列):

  1. 冒泡排序(Bubble Sort)
  2. 选择排序(Selection Sort)
  3. 插入排序(Insertion Sort)
  4. 希尔排序(Shell Sort)
  5. 归并排序(Merge Sort)
  6. 快速排序(Quick Sort)
  7. 堆排序(Heap Sort)
  8. 计数排序(Counting Sort)
  9. 桶排序(Bucket Sort)
  10. 基数排序(Radix Sort)

这些排序算法具有不同的时间复杂度、空间复杂度和稳定性,适用于不同的排序场景。每种算法都有其独特的思想和实现方式,您可以根据具体的需求选择适合的排序算法。

C#实现的十大排序算法的示例代码如下:

1、冒泡排序(Bubble Sort):
class BubbleSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = 0; j < n - i - 1; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
2、选择排序(Selection Sort):
class SelectionSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;

        for (int i = 0; i < n - 1; i++)
        {
            int minIndex = i;
            for (int j = i + 1; j < n; j++)
            {
                if (arr[j] < arr[minIndex])
                {
                    minIndex = j;
                }
            }

            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }
}
3、插入排序(Insertion Sort):
class InsertionSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;
        for (int i = 1; i < n; ++i)
        {
            int key = arr[i];
            int j = i - 1;

            while (j >= 0 && arr[j] > key)
            {
                arr[j + 1] = arr[j];
                j = j - 1;
            }

            arr[j + 1] = key;
        }
    }
}
4、希尔排序(Shell Sort):
class ShellSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;

        for (int gap = n / 2; gap > 0; gap /= 2)
        {
            for (int i = gap; i < n; i++)
            {
                int temp = arr[i];
                int j;
                for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
                {
                    arr[j] = arr[j - gap];
                }
                arr[j] = temp;
            }
        }
    }
}
5、归并排序(Merge Sort):
class MergeSort
{
    public static void Sort(int[] arr)
    {
        if (arr.Length <= 1)
            return;

        int mid = arr.Length / 2;
        int[] leftArr = new int[mid];
        int[] rightArr = new int[arr.Length - mid];

        for (int i = 0; i < mid; i++)
        {
            leftArr[i] = arr[i];
        }

        for (int i = mid; i < arr.Length; i++)
        {
            rightArr[i - mid] = arr[i];
        }

        Sort(leftArr);
        Sort(rightArr);
        Merge(leftArr, rightArr, arr);
    }

    private static void Merge(int[] leftArr, int[] rightArr, int[] arr)
    {
        int leftIndex = 0;
        int rightIndex = 0;
        int current = 0;

        while (leftIndex < leftArr.Length && rightIndex < rightArr.Length)
        {
            if (leftArr[leftIndex] <= rightArr[rightIndex])
            {
                arr[current] = leftArr[leftIndex];
                leftIndex++;
            }
            else
            {
                arr[current] = rightArr[rightIndex];
                rightIndex++;
            }
            current++;
        }

        while (leftIndex < leftArr.Length)
        {
            arr[current] = leftArr[leftIndex];
            leftIndex++;
            current++;
        }

        while (rightIndex < rightArr.Length)
        {
            arr[current] = rightArr[rightIndex];
            rightIndex++;
            current++;
        }
    }
}
6、快速排序(Quick Sort):
class QuickSort
{
    public static void Sort(int[] arr, int low, int high)
    {
        if (low < high)
        {
            int pivotIndex = Partition(arr, low, high);
            Sort(arr, low, pivotIndex - 1);
            Sort(arr, pivotIndex + 1, high);
        }
    }

    private static int Partition(int[] arr, int low, int high)
    {
        int pivot = arr[high];
        int i = low - 1;

        for (int j = low; j < high; j++)
        {
            if (arr[j] < pivot)
            {
                i++;
                Swap(arr, i, j);
            }
        }

        Swap(arr, i + 1, high);
        return i + 1;
    }

    private static void Swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
7、堆排序(Heap Sort):
class HeapSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;

        for (int i = n / 2 - 1; i >= 0; i--)
        {
            Heapify(arr, n, i);
        }

        for (int i = n - 1; i > 0; i--)
        {
            Swap(arr, 0, i);
            Heapify(arr, i, 0);
        }
    }

    private static void Heapify(int[] arr, int n, int i)
    {
        int largest = i;
        int left = 2 * i + 1;
        int right = 2 * i + 2;

        if (left < n && arr[left] > arr[largest])
        {
            largest = left;
        }

        if (right < n && arr[right] > arr[largest])
        {
            largest = right;
        }

        if (largest != i)
        {
            Swap(arr, i, largest);
            Heapify(arr, n, largest);
        }
    }

    private static void Swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
8、计数排序(Counting Sort):
class CountingSort
{
    public static void Sort(int[] arr)
    {
        int n = arr.Length;
        int[] output = new int[n];

        int max = arr[0];
        for (int i = 1; i < n; i++)
        {
            if (arr[i] > max)
            {
                max = arr[i];
            }
        }

        int[] count = new int[max + 1];

        for (int i = 0; i < n; i++)
        {
            count[arr[i]]++;
        }

        for (int i = 1; i <= max; i++)
        {
            count[i] += count[i - 1];
        }

        for (int i = n - 1; i >= 0; i--)
        {
            output[count[arr[i]] - 1] = arr[i];
            count[arr[i]]--;
        }

        for (int i = 0; i < n; i++)
        {
            arr[i] = output[i];
        }
    }
}

9、桶排序(Bucket Sort)
using System;
using System.Collections.Generic;

class BucketSort
{
    public static void Sort(int[] arr)
    {
        int minValue = arr[0];
        int maxValue = arr[0];

        for (int i = 1; i < arr.Length; i++)
        {
            if (arr[i] < minValue)
                minValue = arr[i];
            else if (arr[i] > maxValue)
                maxValue = arr[i];
        }

        int bucketSize = maxValue - minValue + 1;
        List<int>[] buckets = new List<int>[bucketSize];

        for (int i = 0; i < bucketSize; i++)
            buckets[i] = new List<int>();

        for (int i = 0; i < arr.Length; i++)
            buckets[arr[i] - minValue].Add(arr[i]);

        int index = 0;
        for (int i = 0; i < bucketSize; i++)
        {
            int[] temp = buckets[i].ToArray();
            if (temp.Length > 0)
            {
                Array.Sort(temp);
                for (int j = 0; j < temp.Length; j++)
                {
                    arr[index] = temp[j];
                    index++;
                }
            }
        }
    }

    static void Main(string[] args)
    {
        int[] arr = { 4, 2, 7, 1, 9, 5, 3, 6, 8 };
        Console.WriteLine("Before sorting:");
        foreach (int element in arr)
            Console.Write(element + " ");

        Sort(arr);
        
        Console.WriteLine("\n\nAfter sorting:");
        foreach (int element in arr)
            Console.Write(element + " ");
        
        Console.ReadLine();
    }
}
10、基数排序(Radix Sort)
using System;

class RadixSort
{
    public static void Sort(int[] arr)
    {
        int max = FindMax(arr);

        for (int exp = 1; max / exp > 0; exp *= 10)
            CountSort(arr, exp);
    }

    public static void CountSort(int[] arr, int exp)
    {
        int n = arr.Length;
        int[] output = new int[n];
        int[] count = new int[10];

        for (int i = 0; i < 10; i++)
            count[i] = 0;

        for (int i = 0; i < n; i++)
            count[(arr[i] / exp) % 10]++;

        for (int i = 1; i < 10; i++)
            count[i] += count[i - 1];

        for (int i = n - 1; i >= 0; i--)
        {
            output[count[(arr[i] / exp) % 10] - 1] = arr[i];
            count[(arr[i] / exp) % 10]--;
        }

        for (int i = 0; i < n; i++)
            arr[i] = output[i];
    }

    public static int FindMax(int[] arr)
    {
        int max = arr[0];
        for (int i = 1; i < arr.Length; i++)
        {
            if (arr[i] > max)
                max = arr[i];
        }
        return max;
    }

    static void Main(string[] args)
    {
        int[] arr = { 170, 45, 75, 90, 802, 24, 2, 66 };
        Console.WriteLine("Before sorting:");
        foreach (int element in arr)
            Console.Write(element + " ");

        Sort(arr);

        Console.WriteLine("\n\nAfter sorting:");
        foreach (int element in arr)
            Console.Write(element + " ");

        Console.ReadLine();
    }
}

        以上代码分别实现了10大算法。请注意,如果需要对其他类型的数据进行排序,需要进行相应的修改。

相关文章:

C# 十大排序算法

以下是常见的十大排序算法&#xff08;按照学习和实现的顺序排列&#xff09;&#xff1a; 冒泡排序&#xff08;Bubble Sort&#xff09;选择排序&#xff08;Selection Sort&#xff09;插入排序&#xff08;Insertion Sort&#xff09;希尔排序&#xff08;Shell Sort&…...

面试之Glide如何绑定Activity的生命周期

Glide绑定Activity生命周期 Glide.with() 下面都是它的重载方法&#xff0c;Context&#xff0c;Activity&#xff0c;FragmentActivity, Fragment, android.app.Fragment fragment,View都可以作为他的参数&#xff0c;内容大同小异&#xff0c;都是先getRetriever&#xff0…...

从 fatal 错误到 sync.Map:Go中 Map 的并发策略

为什么 Go 语言在多个 goroutine 同时访问和修改同一个 map 时&#xff0c;会报出 fatal 错误而不是 panic&#xff1f;我们该如何应对 map 的数据竞争问题呢&#xff1f; 这篇文章将带你一步步了解背后的原理&#xff0c;并引出解决 map 并发问题的方案。 Map 数据竞争 首先…...

Simon算法详解

0.0 Intro 相关的算法&#xff1a; Deutsh-Jozsa算法&#xff1a; 第一个量子算法对经典算法取得指数级加速的算法 美中不足在于只能确定函数是平衡的还是非平衡的&#xff0c;无法确定函数具体的内容&#xff0c;即无法直接解出函数 Bernstein-Vazirani算法&#xff…...

jrebel IDEA 热部署

1 下载 2022.4.1 JRebel and XRebel - IntelliJ IDEs Plugin | Marketplace 2 选择下载好的zip 离线安装IDEA 插件 重启IDEA 3 打开 [Preference -> JRebel & XRebel] 菜单&#xff0c;输入 GUID address 为 https://jrebel.qekang.com/1e67ec1b-122f-4708-87d…...

pdf拆分成各个小pdf的方法

背景:由于某些缘故,一个大的pdf需要拆分成页数少的pdf,或者pdf需要去掉指定页,那么就有必要对pdf进行重新编辑,这里需要用到一个库,直接进行操作即可。 当使用Python时,可以使用PyMuPDF库来拆分PDF文件。以下是一个示例代码, import fitz # PyMuPDF def split_pdf(i…...

IntelliJ IDEA 常用快捷键一览表(通用型,提高编写速度,类结构、查找和查看源码,替换与关闭,调整格式)

文章目录 IntelliJ IDEA 常用快捷键一览表1-IDEA的日常快捷键第1组&#xff1a;通用型第2组&#xff1a;提高编写速度&#xff08;上&#xff09;第3组&#xff1a;提高编写速度&#xff08;下&#xff09;第4组&#xff1a;类结构、查找和查看源码第5组&#xff1a;查找、替换…...

MSVS C# Matlab的混合编程系列2 - 构建一个复杂(含多个M文件)的动态库:

前言: 本节我们尝试将一个有很多函数和文件的Matlab算法文件集成到C#的项目里面。 本文缩语: MT = Matlab 问题提出: 1 我们有一个比较复杂的Matlab文件: 这个MATLAB的算法,写了很多的算法函数在其他的M文件里面,这样,前面博客的方法就不够用了。会报错: 解决办法如下…...

上位机图像处理和嵌入式模块部署(qt图像处理)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 很多人一想到图像处理&#xff0c;本能的第一反应就是opencv&#xff0c;这也没有错。但是呢&#xff0c;这里面还是有一个问题的&#xff0c;不知…...

AI教我学编程之C#类的实例化与访问修饰符

前言 在这篇文章中&#xff0c;我将带大家深入了解C#编程语言的核心概念&#xff0c;包括类的实例化、访问修饰符的应用&#xff0c;以及C#中不同数据类型的默认值。我会通过逐步分析和具体实例&#xff0c;详细解释如何在C#中正确创建和操作对象&#xff0c;并探讨如何通过访…...

【笔记】Blender4.0建模入门-3物体的基本操作

Blender入门 ——邵发 3.1 物体的移动 演示&#xff1a; 1、选中一个物体 2、选中移动工具 3、移动 - 沿坐标轴移动 - 在坐标平面内移动 - 自由移动&#xff08;不好控制&#xff09; 选中物体&#xff1a;右上的大纲窗口&#xff0c;点击物体名称&#xff0c;物体的轮…...

一文详解 Berachain 测试网:全面介绍与教程,bitget wallet教程

什么是Berachain&#xff1f; Berachain&#xff08;web3.bitget.com/zh-CN/assets/berachain-wallet&#xff09;是一种尖端区块链技术&#xff0c;使用 Cosmos SDK 构建的 Layer-1&#xff0c;兼容以太坊虚拟机&#xff08;EVM&#xff09;。它基于一种独特的概念&#xff0c…...

小程序使用echarts图表-雷达图

本文介绍下小程序中如何使用echarts 如果是通过npm安装&#xff0c;这样是全部安装的&#xff0c;体积有点大 我这边是使用echarts中的一个组件来实现的&#xff0c;下边是具体流程&#xff0c;实际效果是没有外边的红色边框的&#xff0c;加红色边框的效果是这篇说明 1.echa…...

MacM1Pro Parallels19.1.0 CentOS7.9 Install PostgrepSQL

相关阅读 MacM1Pro安装 Parallels Desktop 19.1.0 https://blog.csdn.net/qq_41594280/article/details/135420241 MacM1Pro Parallels安装Parallels Tools https://blog.csdn.net/qq_41594280/article/details/135398780 MacM1Pro Parallels安装CentOS7.9 https://blog.csdn.n…...

Golang 中如何实现 Set

在Go编程中&#xff0c;数据结构的选择对解决问题至关重要。本文将探讨如何在 GO 中实现 set 和 bitset 两种数据结构&#xff0c;以及它们在Go中的应用场景。 Go 的数据结构 Go 内置的数据结构并不多。工作中&#xff0c;我们最常用的两种数据结构分别是 slice 和 map&#…...

记录一下uniapp 集成腾讯im特别卡(已解决)

uniapp的项目运行在微信小程序 , 安卓 , ios手机三端 , 之前这个项目集成过im,不过版本太老了,0.x的版本, 现在需要添加客服功能,所以就升级了 由于是二开 , 也为了方便 , 沿用之前的webview嵌套腾讯IM的方案 , 选用uniapp集成ui ,升级之后所有安卓用户反馈点击进去特别卡,几…...

React16源码: React中的updateHostRoot的源码实现

HostRoot 的更新 1 &#xff09;概述 HostRoot 是一个比较特殊的节点, 因为在一个react应用当中它只会有一个 HostRoot, 它对应的 Fiber 对象是我们的 RootFiber 对象重点在于它的更新过程 2 &#xff09;源码 定位到 packages/react-reconciler/src/ReactFiberBeginWork.js…...

Template -- React

React 版本 Node 21.6.0Npm 10.2.4 项目 创建 npm init vite 项目名称reactjsnpm inpm run dev 依赖 npm i axios # 网络 npm i antd --save # UI npm i ant-design/icons npm i react-router-dom # 路由npm i sass -D # …...

HTML 入门手册(一)

目录 HTML 1-基础语法 单标签 双标签 整体结构 2-标题和水平线 标题 水平线 3-段落和换行 段落 换行 4-列表 无序列表 有序列表 嵌套列表 5-div和span div span 6-格式化标签 粗体 斜体 下划线中划线 上标和下标 7-超链接(a标签) 链接到URL 链接到本…...

GPT帮我快速解决工作上的问题案例

Python入门容易&#xff0c;但精通不易。自从跟着郭老师学Python后&#xff0c;工作中也想偷点懒&#xff0c;之前排班表的问题一直困扰着我&#xff0c;福音来了&#xff0c;现在随着郭老师的小蜜蜂AI出来&#xff0c;说干就干。马上来到郭老师为我们提供的AI网站&#xff1a;…...

从Ping命令到IP分片:用H3C Cloud Lab复现经典网络实验(含Wireshark配置)

从Ping命令到IP分片&#xff1a;用H3C Cloud Lab复现经典网络实验&#xff08;含Wireshark配置&#xff09; 当你按下回车键执行ping 192.168.1.1时&#xff0c;看似简单的动作背后隐藏着一场精密的协议交响乐。作为计算机网络学习者&#xff0c;真正理解IP协议运作机制的最佳方…...

从原理到应用:寄存器二分频电路在FPGA设计中的5种实际场景

从原理到应用&#xff1a;寄存器二分频电路在FPGA设计中的5种实际场景 在FPGA开发中&#xff0c;时钟管理一直是工程师们需要面对的核心挑战之一。想象一下&#xff0c;当你需要在同一个设计中同时处理高速数据流和低速外设通信时&#xff0c;如何优雅地协调不同速度的时钟域&a…...

StructBERT-中文-large部署案例:边缘设备(Jetson Orin)低功耗运行实测

StructBERT-中文-large部署案例&#xff1a;边缘设备&#xff08;Jetson Orin&#xff09;低功耗运行实测 1. 项目背景与模型介绍 StructBERT中文文本相似度模型是一个专门针对中文文本匹配任务优化的深度学习模型。该模型基于structbert-large-chinese预训练模型&#xff0c…...

构建智能搜索引擎:文脉定序系统核心排序模块集成实战

构建智能搜索引擎&#xff1a;文脉定序系统核心排序模块集成实战 你是不是也遇到过这样的烦恼&#xff1f;自己搭建的站内搜索&#xff0c;用户搜“苹果手机”&#xff0c;结果却先蹦出来一堆“苹果水果”的页面。传统的基于关键词匹配的搜索引擎&#xff0c;就像个眼神不太好…...

告别断网烦恼!Android智能家居场景下的Wi-Fi双连接避坑指南

告别断网烦恼&#xff01;Android智能家居场景下的Wi-Fi双连接避坑指南 智能家居生态的爆发式增长让家庭网络环境变得前所未有的复杂。当您试图通过手机App控制客厅的智能灯泡时&#xff0c;却发现因为连接了厨房智能冰箱的本地Wi-Fi而失去了互联网访问权限——这种尴尬场景正在…...

多模型融合展示:cv_resnet101_face-detection与人脸关键点、属性分析模型联动效果

多模型融合展示&#xff1a;cv_resnet101_face-detection与人脸关键点、属性分析模型联动效果 你有没有想过&#xff0c;一张普通的照片背后&#xff0c;藏着多少关于“人”的信息&#xff1f;比如&#xff0c;照片里的人脸在哪里、眼睛鼻子嘴巴的位置、大概多大年纪、是男是女…...

百川2-13B模型模拟技术面试官:涵盖Python入门到进阶的交互式测评

百川2-13B模型模拟技术面试官&#xff1a;涵盖Python入门到进阶的交互式测评 最近在琢磨怎么高效地评估自己的Python水平&#xff0c;是刷题库还是看面经&#xff1f;感觉都差点意思。直到我尝试用百川2-13B模型搭建了一个“虚拟技术面试官”&#xff0c;体验下来&#xff0c;…...

人脸识别OOD模型在娱乐行业的应用:明星识别系统

人脸识别OOD模型在娱乐行业的应用&#xff1a;明星识别系统 1. 引言 想象一下这样的场景&#xff1a;你正在观看一部新上映的电影&#xff0c;突然发现一个熟悉的面孔&#xff0c;但就是想不起来是谁。或者你在刷短视频时&#xff0c;看到一个明星的早期作品&#xff0c;却无…...

从论文复现到R包开发:我是如何把ggrcs和cut.tab2.0应用到NHANES心血管研究中的

从论文复现到R包开发&#xff1a;ggrcs与cut.tab2.0在NHANES心血管研究中的实战应用 临床研究中剂量-反应关系的非线性特征常隐藏着关键医学发现。血清25-羟维生素D与心血管死亡率之间的L型关联正是这类现象的典型代表——当浓度低于54.4 nmol/L时&#xff0c;每单位下降都会显…...

嵌入式Linux资源评估:内存、存储、CPU与进程量化方法

1. 嵌入式Linux系统资源评估方法论在嵌入式Linux平台选型与系统预研阶段&#xff0c;硬件资源评估是决定项目可行性与长期稳定性的关键环节。不同于通用服务器或桌面系统&#xff0c;嵌入式设备通常面临内存容量受限、存储空间紧张、CPU算力有限、功耗约束严格等多重约束条件。…...